Hello,
Welcome to Microsoft Q&A!
How to bind a parameter to Button?
There are not enough information about your scenario so I could just talk from a simple scenario. Based on your code, you are trying to pass the ID
as parameter which should be an int value or a string value.
Then you could try a simple way to do this. You could set the Tag property of the button, bind it to the ID
property that you wan to use.
When the click event is fired, you could get the button object and get the Tag value which should be the same value as ID.
Xaml:
<ScrollViewer Width="800" HorizontalAlignment="Center" VerticalAlignment="Center">
<ItemsRepeater Width="300" x:Name="itemsRepeater1" HorizontalAlignment="Center" VerticalAlignment="Center" >
<DataTemplate x:DataType="local:TestModel">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="47"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Content="{x:Bind Name}" Click="myButton_Click" Tag="{x:Bind Id}" />
</Grid>
</DataTemplate>
</ItemsRepeater>
</ScrollViewer>
Code-behind:
private void myButton_Click(object sender, RoutedEventArgs e)
{
Button button = (Button)sender;
var tag = button.Tag;
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.