WPF DataGrid 无法更改所选行:卡在第一行
简而言之,我的问题是 WPF DataGrid 不允许我选择第一行以外的任何内容。当我第一次填充数据时,没有选择任何行。接下来,无论我点击哪一行,第一行都会亮起。当我使用 OneWayToSource 绑定 SelectedItem 属性时,我发现确实选择了正确行。如果我选择再次单击不同的行,则不会发生任何情况:UI 仍停留在第一行,并且 SelectedItem 属性保留之前的正确值。此时,我必须按住 Ctrl 键单击第一行来取消选择。这让我可以重复上面的情况。
换句话说: - 用户界面似乎与实际选择的内容不同步。 - 需要按住 Ctrl 键单击才能取消选择。 - 即使 SelectedItem 属性指示选择了不同的行,也必须在第一行上执行 Ctrl 键单击。
我知道这是奇怪的行为。我试图在一个单独的项目中复制它,但没有成功。因此,我只是把它扔在那里,看看是否有人有任何想法为什么会这样?
我在单独的项目中唯一没有重复的事情是使用 MEF 进行 View/ViewModel 连接。其他一切都一样。
编辑:我刚刚用 ListBox 替换了所述 DataGrid,并且遇到了同样的问题。我正在使用 Snoop 来尝试找出可以应用于控件的内容,从而极大地改变其行为。
In short, my problem is that the WPF DataGrid won't let me select anything other than the first row. When I first fill it with data, no row is selected. Next, no matter what row I click on, the first row lights up. When I bind the SelectedItem property using OneWayToSource, I see that the correct row was indeed selected. If I choose to click again on a different row, nothing happens: the UI remains stuck on the first row and the SelectedItem property retains the previously correct value. At this point, I have to ctrl-click the first row to deselect things. This allows me to repeat the above situation.
In other words:
- the UI seems to be out of sync with what is actually selected.
- Ctrl-click is required to de-select the selection.
- Ctrl-click must be executed on the first row even if the SelectedItem property indicates that a different row is selected.
I understand this is bizarre behavior. I've tried to duplicate it in a separate project with no success. As such, I'm just throwing this out there to see if anyone has any ideas why it might be acting this way?
The only things that I have not duplicated in my separate project is the use of MEF for the View/ViewModel hookup. Everything else is the same.
EDIT: I just replaced said DataGrid with a ListBox and am experiencing the same problem. I'm using Snoop to try to figure out what might be applied to the control that would change its behavior so much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的问题是我添加到 DataGrid 和 ListBox 的对象已经覆盖了 Equals() 和 GetHashCode() 函数。这些确实搞砸了两个控件呈现所选项目的方式,因此解释了 SelectedItem 问题。最后,通过简单地删除/修复这些覆盖来解决所有问题。
哇!
My problem was that the objects that I was adding to the DataGrid and ListBox had overridden Equals() and GetHashCode() functions. These really screwed up the way both controls rendered which item was selected, therefore explaining the SelectedItem issue. In the end, all the problems were solved by simply removing/fixing those overrides.
Woot!