使用选项卡按钮重新输入后 DataGrid CurrentItem != SelectedItem

发布于 2025-01-05 17:20:39 字数 1070 浏览 1 评论 0原文

这个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入怼 2025-01-12 17:20:39

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

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

IsSynchronizedWithCurrentItem 设置为 true 将使控件的 SelectedItem 与集合的 CurrentItem 属性同步,但是我遇到了问题这是因为我并不总是理解 CurrentItem 如何获取并维护其值。

Usually I bind the SelectedItem to a property in the DataContext, and set IsSynchronizedWithCurrentItem to 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.

酒绊 2025-01-12 17:20:39

解决此问题的方法有两种:

  1. 向 Microsoft 支持记录错误报告,指出在使用 TAB 时 IsSynchronizedWithCurrentItem 并不总是有效。

  2. 将 SelectedItem 绑定到当前单元格的行,该行存储在 CurrentCell 的 Item 属性中:

    >
    

Two ways to resolve this:

  1. Log a bug report with Microsoft Support, stating that IsSynchronizedWithCurrentItem doesn't always work when you use TAB.

  2. Bind the SelectedItem to the current cell's row, which is stored in the CurrentCell's Item property:

    <DataGrid SelectedItem="{Binding RelativeSource={RelativeSource Self}, Path=CurrentCell.Item, Mode=OneWay}" />
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文