我在datatemplate中有一个按钮。单击按钮时,我想将按钮的ID发送到我的命令。下面的片段显然不起作用。我究竟做错了什么?
<DataTemplate>
<Button CommandParameter="ProductId" x:Name="btnProduct" Width="180" Height="40" Content="{Binding DisplayText}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<n:ExecuteCommandAction Command="{Binding ShowSandwichPriceCommand}"
Parameter="{Binding ElementName=btnProduct, Path=SelectedValue}"
/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DataTemplate>
button元素没有SelectedValue属性。
您可能希望使用Name propery或其他可用属性。
<DataTemplate>
<Button CommandParameter="ProductId" x:Name="btnProduct" Width="180" Height="40" Content="{Binding DisplayText}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<n:ExecuteCommandAction Command="{Binding ShowSandwichPriceCommand}"
Parameter="{Binding ElementName=btnProduct, Path=Name}"
/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DataTemplate>
希望这可以帮助
我能够让我的代码使用它。谢谢。
<DataTemplate>
<Button Command="{Binding DataContext.ShowSandwichPriceCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ItemsControl}}}"
CommandParameter="{Binding Path=ProductId}"
Content="{Binding DisplayText}">
</Button>
</DataTemplate>