2012-02-15 53 views
4

这种简单的WPF的DataGrid中的DataGrid CURRENTITEM =的SelectedItem与制表键

<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未被更改时显示的第一个对象。所以CurrentItem不会与SelectetItem一起在DataGrid中看到的状态中。我认为对自己有什么好处...

我的疑问是:如何建议DataGrid具有相同的CurrentItem之前被选中的焦点丢失?以及如何同步CurrentItem和SelectedItem?

我希望能有一个简单的解决方案!你会帮助我很多。在此先感谢...

回答

2

通常我将SelectedItem绑定到DataContext中的一个属性,并将IsSynchronizedWithCurrentItem设置为false。

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

设置IsSyncrhonizedWithCurrentItem为true,将使它这样控制的SelectedItem与集合的CurrentItem财产同步,但我有问题,与此,因为我并不总是知道如何CurrentItem获取和维护它的价值。

+0

谢谢您的帮助!但是恐怕你的建议不能解决问题:在使用Tab按钮重新登录后,我仍然在第一列看到这个丑陋的小框标记CurrentItem行。并且,如果用户使用键盘在DataGrid中导航,则所选行将更改为CurrentItem行,但似乎SelectedItem始终跟随CurrentItem ... – navigato 2012-02-15 23:27:40

0

两种方式解决此问题:

  1. 记录错误报告与Microsoft支持,指出当您使用TAB IsSynchronizedWithCurrentItem并不总是奏效。

  2. 绑定的SelectedItem到当前单元格的列,其存储在CurrentCell的项目属性:

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