c - WPF 中的 SelectedRow 和 DataBoundItem 是如何获取的?

标签 c wpf datagrid

我正在将在 Windows 窗体中进行的客户端注册的基本设计传递给 WPF,我在代码中遇到了一个问题,即 WPF 没有 selectedRow,然后立即还有 DataBoundItem。 有谁知道我如何为 WPF 转录这段代码??

Customer clienteSelecionado = (dataGridPrincipal.SelectedRows [0] .DataBoundItem as Customer);

dataGridPrincipal:是我的数据网格

最佳答案

假设您的 DataGrid 的 UI 是这样的:

    <DataGrid Name="dataGridPrincipal" Grid.Row="0" 
              ItemsSource="{Binding MyList}" 
              SelectedItem="{Binding MyItem , Mode=TwoWay}"
              />

后面的代码如下:

    public ObservableCollection<Customer> MyList { get; set; }
    public Customer MyItem { get; set; } // with INotifyPropertyChanged implemented
    public MainWindow()
    {
        InitializeComponent();
        MyList = YourDataProvider.YourData();
        DataContext = this;
    }

那么这就是您要找的:

        var clienteSelecionado = dataGridPrincipal.SelectedItems[0] as Customer;

关于c - WPF 中的 SelectedRow 和 DataBoundItem 是如何获取的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30689416/

相关文章:

c - 这种对 int64_t 的处理是 GCC 和 Clang 错误吗?

c - 用 C 在 Bison 中模拟 bool 值

wpf - 为什么要使用 RelayCommand 或 DelegateCommand 而不是仅仅实现 ICommand?

c# - Mode=TwoWay、UpdateSourceTrigger=PropertyChanged 或 LostFocus?

wpf - 如何隐藏 WPF DataGrid 中的列标题?

c - 堆栈指针应该指向顶部的值,还是指向下一个值的位置?

c - C 程序员如何获取已传递给函数的数组的大小?

c++ - 在 C++ 中将父窗体的引用传递给子 UserControl

c# - 远程服务器返回错误 : (550) File unavailable (e. g.,找不到文件,无法访问)

c# - 循环遍历 DataSet 表