如何发送按钮的id点击命令?

问题描述 投票:2回答:2

我在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>
wpf xaml
2个回答
0
投票

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>

希望这可以帮助


0
投票

我能够让我的代码使用它。谢谢。

<DataTemplate>
     <Button Command="{Binding DataContext.ShowSandwichPriceCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ItemsControl}}}" 
          CommandParameter="{Binding Path=ProductId}" 
          Content="{Binding DisplayText}">                                        
     </Button>
</DataTemplate>
© www.soinside.com 2019 - 2024. All rights reserved.