首页 文章

WPF DataGrid获取所有SelectedRows

提问于
浏览
1

对于我的这个项目,必须使用免费的WPF DataGrid(我认为Infragistics库很糟糕,我会在此之后收回它) .

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

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

有什么提示可以通过MVVM干净地完成吗?

2 回答

  • 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

  • 1

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

    博客文章有可下载的示例代码 .

相关问题