我正在将在 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/