【问题标题】:WPF DataGrid Get All SelectedRowsWPF DataGrid 获取所有 SelectedRows
【发布时间】:2012-06-01 16:20:46
【问题描述】:

我的这个项目必须使用免费的 WPF DataGrid(我认为 Infragistics 库很糟糕,我在此之后将其收回)。

看起来 DataGrid 没有干净的 MVVM 友好方式来获取 selectedRows 列表?

我可以将数据绑定到SelectedItem="{Binding SelectedSourceFile}",但这仅显示第一个选定的行。需要能够获取所有选定的行。

有任何提示可以通过 MVVM 干净利落地完成吗?

【问题讨论】:

    标签: .net wpf datagrid rows selected


    【解决方案1】:

    您可以使用我为此类情况创建的解决方法,允许您为只读依赖项属性执行OneWayToSource 绑定。我称之为PushBinding

    我在这里写了一篇关于它的博文:OneWayToSource Binding for ReadOnly Dependency Property

    要绑定SelectedItems,可以这样做

    <DataGrid ItemsSource="{Binding ...}">
        <pb:PushBindingManager.PushBindings>
            <pb:PushBinding TargetProperty="SelectedItems" Path="MySelectedItems"/>
        </pb:PushBindingManager.PushBindings>
    </DataGrid>
    

    以及 ViewModel 中的属性

    public IList MySelectedItems
    {
        get;
        set;
    }
    

    如果您有兴趣,可以在此处使用PushBinding 下载演示项目:PushBindingInStyleDemo.zip

    【讨论】:

      【解决方案2】:

      有一个blog post here 描述了如何对 SelectedItems 属性进行双向绑定。该示例使用了 ListBox,但也可以与 DataGrid 一起使用,因为它们都派生自 MultiSelector。

      博文有可下载的示例代码。

      【讨论】:

      • 它与DataGrid一起工作,但有一个不足之处。当 DataGrid 的 CanUserAddRows 属性设置为 true 时,新行会引发异常,因为新行具有“{NewItemPlaceholder}”作为 DataContext。可能,解决问题很容易,但我是 WPF 的新手。因此,我做不到。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-06-16
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多