Passing a value from a button to a WPF ListView

I have a collection that I'm showing in WPF Listview. I will have an edit button on each line and you will need to pass the identifier to another control on the screen when pressed. I am not going to edit in place, so I am not using Gridview.

How to pass this identifier to another control?

Currently my XAML looks like this.

<ListView Name="uxPackageGroups" ItemsSource="{Binding PackageGroups}" BorderThickness="0" Grid.Row="6" Grid.Column="1" Width="300" BorderBrush="#FF0000E8">
<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanel Name="uxStackPanel" Orientation="Horizontal">
            <Button Content="Edit" Width="50" />
            <Label Content="{Binding Name}" Height="20" Margin="0" Padding="0"/>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>

This WPF newbie thanks you in advance!

+3
source share
3 answers

, . , , DataContext. , .

protected void EditButton_Click(object sender, RoutedEventArgs e)
{
   TextBox textBox = (TextBox)sender;

   int id =  ((TheBounObjectType)textBox.DataContext).Id;
}
+3

, , "Tag" :

<Button Content="Edit" Width="50" Tag={Binding} />

<Button Content="Edit" Width="50" Tag={Binding ID} />

.

+3

:

 <Button Content="Edit" Width="50" 
      Command="{some command here}" 
      CommandParameter="{Binding ID}" />

"some command here" , .

+2

Source: https://habr.com/ru/post/1767571/


All Articles