wpf - 从 WPF ListView 中的按钮传递值

标签 wpf xaml listview

我有一个在 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/

相关文章:

WPF滚动条可调整大小的窗口

java - 无法更新自定义适配器中的项目

wpf - ItemTemplate 和 ItemPanelTemplate 有什么区别?

.net - 如何根据按下的行按钮获取数据网格wpf中的行索引

wpf - 如何使用GridViewComboBoxColumn并允许用户进行编辑?

c# - 如何在UWP ListView中进行分页

C# ListView Detail,高亮单个单元格

c# - Visual Studio 2015 XAML 设计器因 System.Exception 而失败

c# - 体感 SDK 2.0 : how to track one body

xaml - Xamarin.Forms:如何正确使用<OnPlatform>来选择要在TableSection中使用的控件?