[英]WPF MVVM retrieve datagrid selected rows
I have a DataGrid with checkbox implemented on it using this code which I found on the internet. 我有一个带有复选框的DataGrid,使用我在互联网上找到的代码。
<my:DataGrid.RowHeaderTemplate>
<DataTemplate>
<Grid>
<CheckBox IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:DataGridRow}}}" />
</Grid>
</DataTemplate>
</my:DataGrid.RowHeaderTemplate>
But, how can I get the selected rows? 但是,我如何获得所选行? I am using WPF MVVM.
我正在使用WPF MVVM。
since you're using the MVVM pattern you can declare a ViewMode like this: 因为你正在使用MVVM模式,你可以像这样声明一个ViewMode:
public class MyViewModel
{
public ObservableCollection<Prototype> Items { ... }
public Prototype SelectedItem SelectedItem { ... }
}
After, in your datagrid, you can declare binding in this way: 之后,在您的数据网格中,您可以通过以下方式声明绑定:
<DataGrid ItemSource="{Binding Items}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"... />
In your code you can use the "SelectedItem" property to get current selected datagrid row. 在您的代码中,您可以使用“SelectedItem”属性来获取当前选定的数据网格行。 Else if you mean "checked" rows, you can query your observable collection:
否则,如果您的意思是“已检查”行,则可以查询您的可观察集合:
var selectedRows = ViewModel.Items.Where(i => i.IsSelected);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.