WPF DataGrid中获取全部SelectedRows全部、WPF、DataGrid、SelectedRows

2023-09-03 04:29:02 作者:酒燒吢頭

有免费使用WPF的DataGrid(我想Infragistics的库是坏的,我在此之后把它收回)对我的这个项目。

Have to use free WPF DataGrid (I thought Infragistics libraries are bad, I take it back after this) for this project of mine.

看起来像DataGrid中可是没有得到selectedRows名单的清洁MVVM友好的方式?

Looks like DataGrid doesnt have clean MVVM Friendly way of getting the list of selectedRows ?

我可以将数据绑定到的SelectedItem ={结合SelectedSourceFile}但是这只能说明选择行第一。需要能够得到所有选定行。

I can data bind to SelectedItem="{Binding SelectedSourceFile}" but this only shows the 1st selected row. Need to be able to get all selected rows.

任何提示干净利落通过MVVM办呢?

Any hints to do it cleanly via MVVM ?

推荐答案

您可以使用我为这样的情况下创造了一个解决方法,可以让你做 OneWayToSource 绑定为只读依赖属性。我把它叫做 PushBinding

You could use a workaround I created for situations like this that allows you to do OneWayToSource Bindings for readonly dependency properties. I call it PushBinding.

我做了一个博客帖子大约在这里: OneWayToSource绑定为只读依赖项属性

I made a blog post about it here: OneWayToSource Binding for ReadOnly Dependency Property

要绑定 SelectedItems ,你可以这样做

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

和在视图模型属性

public IList MySelectedItems
{
    get;
    set;
}

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

If you're interested, you can download a demo project using PushBinding here: PushBindingInStyleDemo.zip