WPF DataGrid:如何以编程方式更改所选行?
如何以编程方式更改选定的行?
我更改了所选项目和单元格,但我无法弄清楚如何突出显示整行。
注意:当用户使用鼠标或键盘选择一行时,突出显示效果很好。
How do I change the selected row programatically?
I change change the selected item and cell, but I cannot figure out how to get the whole row highlighted.
Note: The highlighting works fine when a user selects a row with mouse or keyboard.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这个< /a> 页面。您需要
SelectionUnit
和SelectionMode
来指定如何在 DataGrid 中完成选择。使用
SelectionUnit = FullRow
和SelectionMode = Single
,用户一次只能选择一行。编辑:尝试后,看起来
DataGrid.SelectedItem[i]
将选择整行。不幸的是,看起来您必须在为 DataGrid 的SelectionChanged
属性创建的事件处理程序中手动设置突出显示。take a look at this page. You need both the
SelectionUnit
andSelectionMode
to specify how the selection is done in the DataGrid.With
SelectionUnit = FullRow
andSelectionMode = Single
, the user can only select one row at a time.edit: after trying it out, it looks as though
DataGrid.SelectedItem[i]
will select an entire row. Unfortunately, it looks as if you will have to manually set the highlight in an event handler that you have to create for theSelectionChanged
property of the DataGrid.似乎 SelectedItem 仅在包含元素(例如 UserControl)的 Loaded 事件之后被拾取。这似乎有效:
如果 YourItemsControl 绑定到其中包含任何项目的集合,上面的代码将显示选定的第一个项目。
It seems that the SelectedItem only gets picked up after the Loaded event of the containg element (for example UserControl). This seems to work:
The code above will show the first item selected if YourItemsControl is bound to a collection that has any items in it.