如何以编程方式清除选择?

时间:2021-11-25 19:42:23

It is a simple task in another grid, but I can't make it happen in WPF DataGrid. There are UnselectAll or UnselectAllCells methods, but don't work. Also, setting SelectedItem = null or SelectedIndex = -1 don't work either.

在另一个网格中,这是一个简单的任务,但是我不能在WPF DataGrid中实现它。有UnselectAll或UnselectAllCells方法,但是不工作。同样,设置SelectedItem = null或SelectedIndex = -1也不起作用。

There is a post here about completely disable selection, but that's not what I want. I just want to clear the current selection (if any) and set a new selection programmatically.

这里有一个关于完全禁用选择的帖子,但这不是我想要的。我只想清除当前的选择(如果有的话)并以编程的方式设置一个新的选择。

4 个解决方案

#1


20  

dataGrid.UnselectAll()

For rows mode

行模式

#2


3  

To clear current selection, you can use this code (as you see it is different whether the mode is Single or Extended)

要清除当前的选择,您可以使用此代码(正如您所看到的,模式是单一的还是扩展的)

if(this.dataGrid1.SelectionUnit != DataGridSelectionUnit.FullRow)
    this.dataGrid1.SelectedCells.Clear();

if (this.dataGrid1.SelectionMode != DataGridSelectionMode.Single) //if the Extended mode
    this.dataGrid1.SelectedItems.Clear();
else 
    this.dataGrid1.SelectedItem = null;

To select new items programmatically, use this code:

要以编程方式选择新项目,请使用以下代码:

if (this.dataGrid1.SelectionMode != DataGridSelectionMode.Single) 
{    //for example, select first and third items
    var firstItem = this.dataGrid1.ItemsSource.OfType<object>().FirstOrDefault();
    var thirdItem = this.dataGrid1.ItemsSource.OfType<object>().Skip(2).FirstOrDefault();

    if(firstItem != null)
        this.dataGrid1.SelectedItems.Add(firstItem);
    if (thirdItem != null)
        this.dataGrid1.SelectedItems.Add(thirdItem);
}
else
    this.dataGrid1.SelectedItem = this.dataGrid1.ItemsSource.OfType<object>().FirstOrDefault(); //the first item

#3


1  

Disabling and reenabling the DataGrid worked for me.

禁用和重新启用DataGrid对我有用。

#4


1  

DataGrid.UnselectAllCells()

It works for me.

它适合我。

#1


20  

dataGrid.UnselectAll()

For rows mode

行模式

#2


3  

To clear current selection, you can use this code (as you see it is different whether the mode is Single or Extended)

要清除当前的选择,您可以使用此代码(正如您所看到的,模式是单一的还是扩展的)

if(this.dataGrid1.SelectionUnit != DataGridSelectionUnit.FullRow)
    this.dataGrid1.SelectedCells.Clear();

if (this.dataGrid1.SelectionMode != DataGridSelectionMode.Single) //if the Extended mode
    this.dataGrid1.SelectedItems.Clear();
else 
    this.dataGrid1.SelectedItem = null;

To select new items programmatically, use this code:

要以编程方式选择新项目,请使用以下代码:

if (this.dataGrid1.SelectionMode != DataGridSelectionMode.Single) 
{    //for example, select first and third items
    var firstItem = this.dataGrid1.ItemsSource.OfType<object>().FirstOrDefault();
    var thirdItem = this.dataGrid1.ItemsSource.OfType<object>().Skip(2).FirstOrDefault();

    if(firstItem != null)
        this.dataGrid1.SelectedItems.Add(firstItem);
    if (thirdItem != null)
        this.dataGrid1.SelectedItems.Add(thirdItem);
}
else
    this.dataGrid1.SelectedItem = this.dataGrid1.ItemsSource.OfType<object>().FirstOrDefault(); //the first item

#3


1  

Disabling and reenabling the DataGrid worked for me.

禁用和重新启用DataGrid对我有用。

#4


1  

DataGrid.UnselectAllCells()

It works for me.

它适合我。