【问题标题】:Stop Datagrid selecting first row by default停止Datagrid默认选择第一行
【发布时间】:2011-03-31 22:46:59
【问题描述】:

我正在使用 Wpf Toolkit DataGrid。每当我将 Itemssource 分配给它时,它的第一个项目就会被选中,并且它的 selectionChanged 事件会被调用。如何阻止它默认选择任何行?

【问题讨论】:

  • 您是否尝试在设置ItemSource 之前/之后将SelectedIndex 属性设置为-1?

标签: c# wpf datagrid


【解决方案1】:

检查您是否设置了IsSynchronizedWithCurrentItem="True",并且您要求设置相同?

<DataGrid IsSynchronizedWithCurrentItem="True" ... 
            
            

如果将此属性设置为 true,则第一项的选择是默认行为。

【讨论】:

  • 我有相反的问题——我想要一种方法来使它默认选择第一行。这个答案仍然有效。
【解决方案2】:

您的 DataGrid 可能会绑定到像 PagedCollectionView 这样具有 CurrentItem 属性的集合。此属性与所选行在两个方向上自动同步。解决方案是将 CurrentItem 设置为 null。你可以这样做:

PagedCollectionView pcv = new PagedCollectionView(collection);
pcv.MoveCurrentTo(null);
dataGrid.ItemsSource = pcv;

这在 Silverlight 中特别有用,它没有 DataGrid.IsSynchronizedWithCurrentItem 属性...

【讨论】:

  • +1 我被这个问题困扰了很长时间,这就是解决方案。 :)
  • 当您需要保持 CollectionViewSource 和 View 之间的同步时,这应该是答案。
  • 谢谢。这对我来说适用于 WinUI 社区工具包数据网格。
【解决方案3】:

HCL 的答案是正确的,但对于像我这样快速而松散的读者来说,它被证明是令人困惑的,我最终花了更多时间四处调查其他事情,然后再回到这里仔细阅读。

<DataGrid IsSynchronizedWithCurrentItem="False" ... 

是我们感兴趣的部分,而不是它的对手!

添加一些我自己的价值: 属性IsSynchronizedWithCurrentItem=True 表示网格的CurrentItem 将与集合的当前项同步。设置IsSynchronizedWithCurrentItem=False 是我们想要的。

对于 Xceed 的 Datagrid 用户(例如本例中的我),那将是 SynchronizeCurrent=False

【讨论】:

    【解决方案4】:

    我尝试了许多不同的方法,但对我有用的是捕获第一个选择事件并通过取消选择数据网格上的所有来“撤消”它。

    这是使这项工作的代码,我希望它对其他人有益:)

    /* Add this inside your window constructor */
    this.myDataGrid.SelectionChanged += myDataGrid_SelectionChanged;
    
    /* Add a private boolean variable for saving the suppression flag */
    private bool _myDataGrid_suppressed_flag = false;
    
    /* Add the selection changed event handler */
    void myDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        /* I check the sender type just in case */
        if (sender is System.Windows.Controls.DataGrid)
        {
             System.Windows.Controls.DataGrid _dg = (System.Windows.Controls.DataGrid)sender;
    
            /* If the current item is null, this is the initial selection event */
             if (_dg.CurrentItem == null)
             {
                  if (!_myDataGrid_suppressed_flag)
                  {
                        /* Set your suppressed flat */
                        _dgRateList_suppressed_flag = true;
                        /* Unselect all */
                        /* This will trigger another changed event where CurrentItem == null */
                        _dg.UnselectAll();
    
                        e.Handled = true;
                        return;
                  }
             }
             else
             {
                    /* This is a legitimate selection changed due to user interaction */
             }
        }
    }
    

    【讨论】:

    • 看起来有点老套,但它确实救了我的命,因为我尝试将 IsSynchronizedWithCurrentItem 设置为“false”无济于事。
    猜你喜欢
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 2021-11-12
    • 2021-03-21
    相关资源
    最近更新 更多