简体   繁体   English

如何使用MVVM从DataGrid获取SelectedItems

[英]How to Get SelectedItems From DataGrid Using MVVM

I am building a WPF application using C# and also i used MVVM architecture in my application. 我正在使用C#构建WPF应用程序,并且在我的应用程序中使用了MVVM体系结构 I created a CheckBox column in telerik gridview by using DataTemplate . 我使用DataTemplate在telerik gridview中创建了一个CheckBox列 I am using a collection to bind the data in the GridView . 我正在使用一个集合来绑定GridView中的数据。

How can i find the particular row number of DataItem has been selected in that Collection When CheckBox is checked on the Grid. 当在网格上选中CheckBox 时,如何找到在该Collection中选择的DataItem特定行号

Here My code for creating CheckBox on Grid is: 在这里我在网格上创建CheckBox的代码是:

<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. 提前致谢。

I would recommend using an MVVM approach and use a Binding to get the selected items. 我建议使用MVVM方法,并使用绑定来获取所选项目。 Unfortunately, the DataGrid doesn't provide a DependencyProperty for selected items, but you can provide your own. 不幸的是,DataGrid没有为选定的项目提供DependencyProperty,但是您可以提供自己的。 Derive a class from DataGrid, register a dependency property for SelectedItems and override the SelectionChanged event to update your dependency property. 从DataGrid派生一个类,为SelectedItems注册一个依赖项属性,并重写SelectionChanged事件以更新您的依赖项属性。 You can then use a Binding to inform your ViewModel of the selected items. 然后,您可以使用Binding通知您的ViewModel所选项目。

Code: 码:

public class CustomDataGrid : DataGrid
{
    public static readonly DependencyProperty CustomSelectedItemsProperty = DependencyProperty.Register(
        "CustomSelectedItems", typeof (List<object>), typeof (CustomDataGrid), 
        new PropertyMetadata(new List<object>()));

    public List<object> CustomSelectedItems
    {
        get { return (List<object>) GetValue(CustomSelectedItemsProperty); }
        set { SetValue(CustomSelectedItemsProperty, value);}
    }

    protected override void OnSelectionChanged(SelectionChangedEventArgs e)
    {
        foreach(var item in e.AddedItems)
            CustomSelectedItems.Add(item);
        foreach (var item in e.RemovedItems)
            CustomSelectedItems.Remove(item);
        base.OnSelectionChanged(e);
    }
}

XAML: XAML:

<Grid>
    <Ctrl:CustomDataGrid CustomSelectedItems="{Binding MySelectedItems}"/>
</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM