简体   繁体   English

使用制表符按钮重新进入后,DataGrid CurrentItem!= SelectedItem

[英]DataGrid CurrentItem != SelectedItem after reentry with tab-button

This simple WPF-DataGrid 这个简单的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). 它显示了一个没有问题的绑定列表,在获得焦点时表现得很奇怪:首先,用户选择了一行,这使得datagrid以选定的方式显示该行(SelectedItem和CurrentItem包含所选对象) 。 Then the focus is given to another control. 然后重点关注另一个控件。 In this status - the selection is still shown - SelectedItem is still there while CurrentItem is null! 在此状态 - 仍然显示选择 - 当CurrentItem为null时,SelectedItem仍然存在! And then the focus comes back by using the TAB-Button. 然后通过使用TAB-Button重新调整焦点。 That makes the CurrentItem to be the first object that is shown while the SelectedItem isn't changed. 这使得CurrentItem成为第一个在SelectedItem未更改时显示的对象。 So CurrentItem doesn't go together with SelectetItem in that state which is to be seen in the DataGrid. 因此,CurrentItem不会与SelectetItem一起处于可在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? 我的qustion是:如何建议DataGrid具有在焦点丢失之前选择的相同CurrentItem? And how is it possible to synchronize CurrentItem und SelectedItem? 如何同步CurrentItem和SelectedItem?

I hope for a simple solution! 我希望有一个简单的解决方案! You would help me a lot. 你会帮助我很多。 Thanks in advance... 提前致谢...

Usually I bind the SelectedItem to a property in the DataContext , and set IsSynchronizedWithCurrentItem to false. 通常我将SelectedItem绑定到DataContext的属性,并将IsSynchronizedWithCurrentItem设置为false。

<DataGrid ItemsSource="{Binding SomeCollection}"
          SelectedItem="{Binding SelectedItem}" />

Setting IsSyncrhonizedWithCurrentItem to true will make it so the SelectedItem of the Control is synchronized with the CurrentItem property of a collection, however I've had issues with this since I don't always understand how CurrentItem gets and maintains its value. IsSyncrhonizedWithCurrentItem设置为true将使得Control的SelectedItem与集合的CurrentItem属性同步,但是我遇到了问题,因为我并不总是理解CurrentItem如何获取并保持其值。

Two ways to resolve this: 解决此问题的两种方法:

  1. Log a bug report with Microsoft Support, stating that IsSynchronizedWithCurrentItem doesn't always work when you use TAB. 使用Microsoft支持记录错误报告,指出在使用TAB时IsSynchronizedWithCurrentItem并不总是有效。

  2. Bind the SelectedItem to the current cell's row, which is stored in the CurrentCell's Item property: 将SelectedItem绑定到当前单元格的行,该行存储在CurrentCell的Item属性中:

     <DataGrid SelectedItem="{Binding RelativeSource={RelativeSource Self}, Path=CurrentCell.Item, Mode=OneWay}" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM