WPF Datagrid选择\取消选择 [英] WPF Datagrid Select\Deselect

查看:485
本文介绍了WPF Datagrid选择\取消选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在带有CheckBox列的WPF UserControl上有一个DataGrid. DataGrid绑定到UserControl的ViewModel中的ObservableCollection.我在UserControl的ViewModel中也有一个SelectedItem属性,DataGridSelectedItem属性绑定到该属性.

我要尝试实现的是,当您选择DataGrid的一行时,选中了CheckBox(显然SelectedItem在ViewModel中已更新).如果然后再次单击同一选定的行,则CheckBox应该变为未选中状态,但是选定的行不得设置为null. (因此Ctrl + Click对我不起作用)

如何获得DataGrid以意识到我已经选择"了已经选择的行?

希望有人对我有解决办法!

谢谢!

Hi all,

I have a DataGrid on a WPF UserControl with a CheckBox column. The DataGrid is bound to an ObservableCollection in the UserControl''s ViewModel. I also have a SelectedItem property in the UserControl''s ViewModel to which the SelectedItem property of the DataGrid is bound.

What I''m attempting to achieve is when you select a row of the DataGrid, the CheckBox get checked (and obviously the SelectedItem is updated in the ViewModel). If you then click on the same selected row a second time, the CheckBox should become unchecked, but the selected row must not be set to null. (so Ctrl+Click is not going to work for me)

How can I get the DataGrid to realise that I have "selected" a row which is already selected?

I hope someone has a solution for me!

Thanks!

推荐答案

没关系.设法自己解决了这个问题.

如果您想知道,请问我,尽管我最终使用的解决方案与我一直在寻找的解决方案有所不同.
Never mind. Managed to solve this on my own.

If you want to know how, ask me, albeit the solution I ended up using is a bit different to the solution i was looking for.


理论上,从纯粹的角度来看,这应该很容易实现关于您绑定到IsSelected的属性的使用.我要做的是包括这样的检查:
Theoretically this should be fairly easy to achieve based purely on the use of the property you have bound to for IsSelected. What I would do is include a check like this:
public bool IsSelected
{
  get { return isSelected; }
  set
  {
    if (isSelected || isSelected != value) return;
    isSelected = value;
    OnChanged("IsSelected");
  }
}

通过包括检查此行的isSelected是否已经为真,我们可以确保不对已设置的行进行尝试.

By including the check to see if isSelected is already true for this row, we can ensure that we don''t try to update the row if we have already set it.


这篇关于WPF Datagrid选择\取消选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆