C# 如何使用SelectionUnit=cell取消选择WPF数据网格中的选定单元格

C# 如何使用SelectionUnit=cell取消选择WPF数据网格中的选定单元格,c#,wpf,datagrid,C#,Wpf,Datagrid,我正在与VS2015合作开发WPF应用程序。 在我的一个WPF窗口中,我得到了一个数据网格,其中SelectionUnit=Cell,SelectionMode=Single。 此外,我还得到了一种方法,可以上下移动数据网格中的行进行排序。 排序工作正常,但问题是,鼠标光标选择的最后一个单元格始终处于可视状态(蓝色背景),这可能会干扰用户。 因此,我尝试通过以下代码行删除单元格的视觉标记: datagrid.UnselectAllCells(); datagrid.SelectedCells.C

我正在与VS2015合作开发WPF应用程序。 在我的一个WPF窗口中,我得到了一个数据网格,其中SelectionUnit=Cell,SelectionMode=Single。 此外,我还得到了一种方法,可以上下移动数据网格中的行进行排序。 排序工作正常,但问题是,鼠标光标选择的最后一个单元格始终处于可视状态(蓝色背景),这可能会干扰用户。 因此,我尝试通过以下代码行删除单元格的视觉标记:

datagrid.UnselectAllCells();
datagrid.SelectedCells.Clear();
这两条线都不适合我。 最后选定的单元格仍处于选中状态

如何删除该选项

任何帮助都将不胜感激

最后是XAML的一个片段,其中包含DataGrid的定义:

<DataGrid x:Name="grdGraphicalElementMatrix" Grid.Row="1" Grid.Column="0"
          HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
          CanUserAddRows="True" 
          IsReadOnly="False"
          AutoGenerateColumns="False"
          SelectionUnit="Cell" SelectionMode="Single"
          CurrentCellChanged="grdGraphicalElementMatrix_CurrentCellChanged"
          ItemsSource="{Binding GraphElemMatrix}">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="colXAssignment"
                            Width="1*"
                            Binding="{Binding Path=X}"
                            Header="X"/>
        <DataGridTextColumn x:Name="colYAssignment"
                            Width="1*"
                            Binding="{Binding Path=Y}"
                            Header="Y"/>
    </DataGrid.Columns>
</DataGrid>

我已经设置了一个测试应用程序,它能够清除DataGrid选择-具有以下功能:

查看

<DockPanel>
    <Button Content="Clear Selected" DockPanel.Dock="Bottom" Command="{Binding ClearGridCommand}" CommandParameter="{Binding ElementName=datagrid}"/>
    <DataGrid x:Name="datagrid" 
            CurrentCell="{Binding SelectedCell, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            SelectionMode="Single" 
            SelectionUnit="Cell" 
            HorizontalAlignment="Stretch" 
            Margin="10" 
            VerticalAlignment="Stretch" 
            ItemsSource="{Binding Customers}" 
            CanUserAddRows="True" 
            IsReadOnly="False"
            AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name"></DataGridTextColumn>
            <DataGridTextColumn Header="No"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</DockPanel>

也许您可以通过在
SelectedCell
中调试setter来获得更多信息。可能正在清除,但通过您的
grdGraphicalElementMatrix\u CurrentCellChanged
方法重新选择?

非常感谢您的帮助和有用提示。 多亏了你,我找到了一个有效的解决方案

首先,我将CurrentCellChanged事件替换为SelectedCellsChanged事件。 然后,我重新编程了向上或向下移动选定行的方法。 这里是新代码,用于取消选择位于旧行索引的单元格,并选择位于新行索引的单元格

// UnselectAllCells was correct.
datagrid.UnselectAllCells();
// But a refresh is needed. This was missing.
datagrid.Items.Refresh();
// Selects the cell in the moved row. Focus is needed, so the cell appears selected all the time.
datagrid.CurrentCell = new DataGridCellInfo(datagrid.Items[newIndex], datagrid.Columns[GrdGraphicalElementMatrixSelColIndex]);
datagrid.SelectedCells.Add(datagrid.CurrentCell);
datagrid.Focus();

您是否使用MVVM模式绑定到DataGrid?如果是这样,我可以建议将SelectedItem属性绑定到模型中的一个属性,您可以将该属性设置为null,它应该取消选中网格中的项。@Johan:Hello Johan。是的,我正在使用MVVM,但由于SelectionUnit Cell,我无法使用SelectedItem。SelectedItem仅适用于SelectionUnit行。是不是我错了?哦,我明白了,行选择不是问题,而是单个单元格的问题?您使用的是哪种数据网格?@Johan:我使用的是Visual Studio 2015的标准数据网格,.NET Framework 4.5.2。您介意在您的问题中添加数据网格的xaml吗?你好,Johan。你是对的。CurrentCellChanged事件导致调用setter。我是否有其他可能在运行时检测所选单元格的行和列索引?您好。我删除了CurrentCellChanged事件,并将用于获取行和列的代码转移到两个属性的getter,通过这两个属性,我无法在其他方法中获取它们。当我启动窗口时,CurrentCell的Setter现在只被调用一次。但当我向上或向下移动一行时,最后选定的单元格仍然显示为选中。很高兴能提供帮助!
private object selectedCell = null;

public object SelectedCell {
    get { return this.selectedCell; }
    set {
        if (this.selectedCell != value) {
            this.selectedCell = value;
            SetPropertyChanged("SelectedCell");
        }
    }
}

public void ClearGrid(object obj) {
    var dg = obj as DataGrid;
    if (dg != null) {
        dg.UnselectAllCells();
    }
}
// UnselectAllCells was correct.
datagrid.UnselectAllCells();
// But a refresh is needed. This was missing.
datagrid.Items.Refresh();
// Selects the cell in the moved row. Focus is needed, so the cell appears selected all the time.
datagrid.CurrentCell = new DataGridCellInfo(datagrid.Items[newIndex], datagrid.Columns[GrdGraphicalElementMatrixSelColIndex]);
datagrid.SelectedCells.Add(datagrid.CurrentCell);
datagrid.Focus();