Silverlight DataGrid 从代码更新 SelectedItem
当我从代码(通过 ViewModel 中的绑定对象)更新数据网格 SelectedItem 时,如何让可视网格突出显示新选定的项目?
谢谢,
马克
更新:这对我来说仍然是一个问题。我的 SelectedItem 属性已经实现了更改通知,但数据网格无法视觉显示已选择的行 - 即它没有突出显示。
When I update a datagrid SelectedItem from code (via a bound object in a ViewModel), how to I get the visual grid to highlight the newly selected item?
Thanks,
Mark
UPDATE: This is still an issue for me. My SelectedItem property already implements change notification, but the datagrid is not VISUALLY displaying which row has been selected - i.e. it is not getting highlighted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜您确实验证了
SelectedItem
已更改(您可以暂时将Binding
模式设置为TwoWay
来看看它是否以相反的方式工作,通过单击该行,您应该看到突出显示和SelectedItem
的set
方法被执行)。如果是,请验证您是否确实与PropertyChanged
方法调用上的属性名称完全匹配。由于您此处的类型不安全,因此您可能犯了拼写错误。如果不是,请检查您的数据绑定属性设置是否正确。另一个想法是,您可能已经更改了DataGrid
的样式或模板,现在您丢失了一些视觉状态。DataGridRow
可以使用样式模板设置样式。您有三种选定状态,称为UnfocusedSelected
(可能是正确的状态)、NormalSelected
和MouseOverSelected
。您可以尝试使用此模板创建自己的视觉状态:
这是来自一个很好的 有关自定义 DataGrid 样式的 MSDN 文章。例如,您可以修改模板的
UnfocusedSelected
部分,并查看是否看到任何更改,例如在其周围添加红色边框或其他内容。也许值得一试。我希望你知道如何应用自己的风格。如果没有,这里是另一个 MSDN 资源。
我知道,这些只是提示,但最终可能会有所帮助。
I guess that you really verified that the
SelectedItem
has changed (you could set theBinding
Mode toTwoWay
temporarily to see if it works the other way round, by clicking the row you should see the highlight andSelectedItem
'sset
-method executed). If yes, verify that you really exactly match the Property Name onPropertyChanged
method invoke. Since you're not typesafe here, you might have made a spelling error. If no, check if your Databinding Attribute is set correctly. Another idea is that you might have changed theDataGrid
's styles or template and now you are missing some of the Visual States. TheDataGridRow
can be styled using a style template. You have three selected states, calledUnfocusedSelected
(probably the right one),NormalSelected
andMouseOverSelected
.You could try to make your own Visual State by using this template:
This is a copy-paste from a good MSDN Article on customizing the DataGrid Styles. You could, for example, modify the
UnfocusedSelected
part of the template and see if you see any change when, for example, adding a red border around it or something.Maybe it's worth a try. I hope that you know how to apply own styles. If not, here is another MSDN Resource.
I know, these are just tips, but maybe helpful at last.
您需要在 ViewModel 上实现
INotifyPropertyChanged
接口,并让其SelectedItem
属性在其值发生更改时调用PropertyChanged
事件。You need to implement the
INotifyPropertyChanged
interface on your ViewModel and have itsSelectedItem
property invoke thePropertyChanged
event when its value is changes.