问题描述
我正在使用WPF数据网格,并且有SelectionUnit =Cell和SelectionMode =Extended。我也试图尽可能多地遵守MVVM主体。我需要我的ViewModel跟踪当前的SelectedCells。
如果我可以将其SelectedCells属性绑定到我的ViewModel,那么生活会很简单。奇怪的是,SelectedCells只提出一次 - 当我们首先选择网格中的任何单元格。
MS在这里解释:
任何人都可以想到一个MVVM友好的方法来解决它?
谢谢!
我意识到我的最后一个答案是SelectedItems而不是SelectedCells,所以我写了完成附加的属性类,为多个SelectedCells进行数据绑定,工作如下:
< controls:DataGrid ItemsSource ={StaticResource list}
SelectionMode =扩展
主题:DataGridHelper.SelectedCells ={Binding Path = myViewModelSourceList,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}
/>
我有一个工作的源代码和一个演示项目。
I'm using the WPF datagrid, and have SelectionUnit="Cell" and SelectionMode="Extended". I'm also trying to adhere to the MVVM principals as much as I can.
I need my ViewModel to keep track of the current SelectedCells.
Life would be easy if I could just Bind its SelectedCells property to my ViewModel. Oddly enough, SelectedCells is only raised once - when we first select any cell in the grid.
MS explains it here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/737117f4-6d20-4232-88cf-e52cc44d4431
Can anyone think of an MVVM-friendly approach to get around it?
Thanks!
I realized my last answer was for SelectedItems instead of SelectedCells, so I wrote a complete attached property class to do data binding for multiple SelectedCells which works as follows:
<controls:DataGrid ItemsSource="{StaticResource list}"
SelectionMode="Extended"
themes:DataGridHelper.SelectedCells="{Binding Path=myViewModelSourceList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
/>
I have a working source code and a demo project of it here.
这篇关于WPF Datagrid:将SelectedCells绑定到我的ViewModel的MVVM友好的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!