使用选项卡按钮重新输入后 DataGrid CurrentItem != SelectedItem
这个简单的 WPF-DataGrid
<DataGrid AutoGenerateColumns="False" Height="300" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Name="dgOriginal" Margin="4,12,0,0"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" IsSynchronizedWithCurrentItem="True"
CanUserSortColumns="False" SelectionMode="Single" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridCheckBoxColumn x:Name="col2Checked"/>
<DataGridTextColumn x:Name="col2Name"/>
<DataGridTextColumn x:Name="col2Vorname"/>
</DataGrid.Columns>
它显示了一个没有问题的绑定列表,当重新获得焦点时,其行为方式很奇怪: 首先,用户选择一行,这使得数据网格以所选方式显示该行(SelectedItem 和 CurrentItem 包含所选对象)。然后焦点被赋予另一个控件。在此状态下 - 仍然显示选择 - SelectedItem 仍然存在,而 CurrentItem 为空!然后使用 TAB 按钮返回焦点。这使得 CurrentItem 成为在 SelectedItem 未更改时显示的第一个对象。因此,在 DataGrid 中看到的那种状态下,CurrentItem 不会与 SelectetItem 一起出现。我自己认为这有什么好处...
我的问题是:如何建议 DataGrid 拥有与失去焦点之前选择的相同的 CurrentItem?如何同步 CurrentItem 和 SelectedItem?
我希望有一个简单的解决方案!你会给我很多帮助。提前致谢...
This simple WPF-DataGrid
<DataGrid AutoGenerateColumns="False" Height="300" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Name="dgOriginal" Margin="4,12,0,0"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" IsSynchronizedWithCurrentItem="True"
CanUserSortColumns="False" SelectionMode="Single" SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridCheckBoxColumn x:Name="col2Checked"/>
<DataGridTextColumn x:Name="col2Name"/>
<DataGridTextColumn x:Name="col2Vorname"/>
</DataGrid.Columns>
which shows a binded list without problems, behaves in a strange way when getting the focus back:
First of all a row is selected by the user which makes the datagrid show that row in the selected way (SelectedItem and also CurrentItem contain the selected object). Then the focus is given to another control. In this status - the selection is still shown - SelectedItem is still there while CurrentItem is null! And then the focus comes back by using the TAB-Button. That makes the CurrentItem to be the first object that is shown while the SelectedItem isn't changed. So CurrentItem doesn't go together with SelectetItem in that state which is to be seen in the DataGrid. And I think to myself whats that good for...
My qustion is: How to advice the DataGrid to have the same CurrentItem that was selected before the focus was lost? And how is it possible to synchronize CurrentItem und SelectedItem?
I hope for a simple solution! You would help me a lot. Thanks in advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常我将
SelectedItem
绑定到DataContext
中的属性,并将IsSynchronizedWithCurrentItem
设置为 false。将 IsSynchronizedWithCurrentItem 设置为 true 将使控件的
SelectedItem
与集合的CurrentItem
属性同步,但是我遇到了问题这是因为我并不总是理解CurrentItem
如何获取并维护其值。Usually I bind the
SelectedItem
to a property in theDataContext
, and setIsSynchronizedWithCurrentItem
to false.Setting IsSyncrhonizedWithCurrentItem to true will make it so the
SelectedItem
of the Control is synchronized with theCurrentItem
property of a collection, however I've had issues with this since I don't always understand howCurrentItem
gets and maintains its value.解决此问题的方法有两种:
向 Microsoft 支持记录错误报告,指出在使用 TAB 时 IsSynchronizedWithCurrentItem 并不总是有效。
将 SelectedItem 绑定到当前单元格的行,该行存储在 CurrentCell 的 Item 属性中:
Two ways to resolve this:
Log a bug report with Microsoft Support, stating that IsSynchronizedWithCurrentItem doesn't always work when you use TAB.
Bind the SelectedItem to the current cell's row, which is stored in the CurrentCell's Item property: