如何使用选择单元 = 单元格在 WPF 数据网格中取消选择选定的单元格



我正在WPF应用程序上使用VS2015。 在我的一个 WPF 窗口中,我得到了一个 DataGrid,其中 SelectionUnit = Cell 和 SelectionMode = Single。 此外,我得到了一种方法来上下移动 DataGrid 中的行以进行排序。 排序有效,但问题是鼠标光标选择的最后一个单元格在视觉上始终处于选中状态(蓝色背景(,这可能会打扰用户。 所以我试图通过以下代码行删除单元格的视觉标记:

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>

grdGraphicalElementMatrix_CurrentCellChanged是一种方法,当用户单击其中一个单元格以选择它时,我可以获取所选行和列。

private void grdGraphicalElementMatrix_CurrentCellChanged(object sender, EventArgs e)
{
if (grdGraphicalElementMatrix.CurrentCell != null && grdGraphicalElementMatrix.CurrentCell.Column != null)
{
vm.GrdGraphicalElementMatrixSelColIndex = grdGraphicalElementMatrix.CurrentCell.Column.DisplayIndex;
vm.GrdGraphicalElementMatrixSelRowIndex = grdGraphicalElementMatrix.Items.IndexOf(grdGraphicalElementMatrix.CurrentItem);
}
}

我已经设置了一个测试应用程序,该应用程序能够清除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>

注意 我正在通过命令参数将数据网格传递到按钮命令

数据上下文

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();
}
}

也许您可以通过调试SelectedCell中的setter来获取更多信息。也许它正在清除,但通过您的grdGraphicalElementMatrix_CurrentCellChanged方法重新选择?

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

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

// 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();

最新更新