如何使用 MVVM 从 DataGrid 获取选定项
我正在使用 C# 构建一个 WPF 应用程序,并且在我的应用程序中使用了 MVVM 架构。 我使用 DataTemplate 在 telerik gridview 中创建了一个 CheckBox 列。我正在使用集合来绑定 GridView 中的数据。
当在网格上选中复选框时,如何找到该集合中已选择的 DataItem 的特定行号。
这里我在网格上创建复选框的代码是:
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<CheckBox Name="StockCheckBox" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type telerik:GridViewRow}}, Path=IsSelected}" />
</StackPanel>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
我的收藏是,
foreach (var AvailableStock in AvailableStocks)// In this **AvailableStocks**(IEnumurable Collection) I got all the datas in the Gridview
//In this collection How can i know that the particular RowItem is selected in that gridview by CheckBox
{
if (SelectedStock != null)
{
this.SelectedStocks.Add(AvailableStock );
this.RaisePropertyChanged(Member.Of(() => AvailableStocks));
}
}
任何人请告诉我一些关于这个的建议我怎样才能实现这个? 如何识别已选择的特定行?
提前致谢。
I am building a WPF application using C# and also i used MVVM architecture in my application.
I created a CheckBox column in telerik gridview by using DataTemplate. I am using a collection to bind the data in the GridView .
How can i find the particular row number of DataItem has been selected in that Collection When CheckBox is checked on the Grid.
Here My code for creating CheckBox on Grid is:
<telerik:GridViewDataColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<CheckBox Name="StockCheckBox" IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type telerik:GridViewRow}}, Path=IsSelected}" />
</StackPanel>
</DataTemplate>
</telerik:GridViewDataColumn.CellTemplate>
And My Collection is,
foreach (var AvailableStock in AvailableStocks)// In this **AvailableStocks**(IEnumurable Collection) I got all the datas in the Gridview
//In this collection How can i know that the particular RowItem is selected in that gridview by CheckBox
{
if (SelectedStock != null)
{
this.SelectedStocks.Add(AvailableStock );
this.RaisePropertyChanged(Member.Of(() => AvailableStocks));
}
}
Anyone Please tell me some suggestion on this How can i achieve this? How can i identify that particular row has been selected?
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用 MVVM 方法并使用 Binding 来获取所选项目。遗憾的是,DataGrid 不为所选项目提供 DependencyProperty,但您可以提供自己的。从 DataGrid 派生一个类,注册 SelectedItems 的依赖属性并重写 SelectionChanged 事件以更新依赖属性。然后,您可以使用 Binding 来通知您的 ViewModel 所选项目。
代码:
XAML:
I would recommend using an MVVM approach and use a Binding to get the selected items. Unfortunately, the DataGrid doesn't provide a DependencyProperty for selected items, but you can provide your own. Derive a class from DataGrid, register a dependency property for SelectedItems and override the SelectionChanged event to update your dependency property. You can then use a Binding to inform your ViewModel of the selected items.
Code:
XAML: