开发者

how to send id of button clicked to command?

I've got a button inside a datatemplate. When the button is clicked I'd like to send the id of the button to my command. The snippet below obviously dosn't work.开发者_如何学编程 What am i doing wrong?

<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>


The button element doesn't have the SelectedValue property.

You might want to use the Name propery instead or some other available property.

<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>

Hope this helps


i was able to get my code working with this. thanks.

<DataTemplate>
     <Button Command="{Binding DataContext.ShowSandwichPriceCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ItemsControl}}}" 
          CommandParameter="{Binding Path=ProductId}" 
          Content="{Binding DisplayText}">                                        
     </Button>
</DataTemplate>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜