我有一个在 WPF Listview 中显示的集合。我将在每一行中都有一个编辑按钮,并且需要在单击该按钮时将一个 ID 传递给屏幕上的另一个控件。我不会在原地进行编辑,所以我不会使用 Gridview。
如何将此 ID 传递给我的其他控件?
目前我的 XAML 看起来像这样。
<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>
这个WPF新手提前谢谢你!
最佳答案
您正在使用数据绑定(bind),所以这很容易。在响应按钮单击的代码中,获取对按钮的引用并检查其 DataContext 属性。这将引用您绑定(bind)到的基础对象。
protected void EditButton_Click(object sender, RoutedEventArgs e)
{
TextBox textBox = (TextBox)sender;
int id = ((TheBounObjectType)textBox.DataContext).Id;
}
关于wpf - 从 WPF ListView 中的按钮传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3841069/