开发者

Prevent users from changing DataGrid's SelectedItem

I have a WPF 4.0 application and I am using the WPF DataGrid. What I want is to use navigation buttons on my view to change the SelectedItem the DataGrid, rather than letting the user change the开发者_运维百科 SelectedItem by clicking on the DataGrid. I am using an ICollectionView in my ViewModel as the ItemsSource for my DataGrid. Here is what I have so far:

NextCommand (ViewModel):

DefaultView.MoveCurrentToNext();  // DefaultView is an ICollectionView
SelectedItem = DefaultView.CurrentItem as MyProperty;

DataGrid (View):

<DataGrid ItemsSource="{Binding Path=DefaultView}"
          SelectedItem="{Binding Path=SelectedItem}"
          IsSynchronizedWithCurrentItem="True">
    ...
</DataGrid>

The navigation buttons work great... however, I do not want to allow the user to click on the DataGrid to change the SelectedItem. Any ideas on how to accomplish this? I've played around with the DataGrid_SelectionChanged event, but the problem is that the binding on the SelectedItem updates the ViewModel before this event even fires. I would prefer that the SelectedItem does not get changed twice (once when the user clicks, and twice when it is set back to the original). I am ok with using the Code behind if needed...


Apparently you have to disable it through a custom DataGrid Template:

http://www.wpfsharp.com/2011/05/how-to-disable-row-selection-in-a-wpf-datagrid/

Out of curiosity, why do you want to do this? I mean why do you want to forbid a user from selecting a row by clicking it, but yet allow the user to select a row indirectly by using some navigation buttons?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜