如何使用 MVVM 从 DataGrid 获取选定项

发布于 2025-01-05 20:50:52 字数 1535 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韵柒 2025-01-12 20:50:52

我建议使用 MVVM 方法并使用 Binding 来获取所选项目。遗憾的是,DataGrid 不为所选项目提供 DependencyProperty,但您可以提供自己的。从 DataGrid 派生一个类,注册 SelectedItems 的依赖属性并重写 SelectionChanged 事件以更新依赖属性。然后,您可以使用 Binding 来通知您的 ViewModel 所选项目。

代码:

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:

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

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:

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:

<Grid>
    <Ctrl:CustomDataGrid CustomSelectedItems="{Binding MySelectedItems}"/>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文