【问题标题】:How to unselect selected cell in WPF DataGrid with SelectionUnit = Cell如何使用 SelectionUnit = Cell 取消选择 WPF DataGrid 中的选定单元格
【发布时间】:2018-06-28 07:49:56
【问题描述】:

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

datagrid.UnselectAllCells();
datagrid.SelectedCells.Clear();

这两行都不适合我。 最后选中的单元格仍处于选中状态。

如何删除该选择?

任何帮助将不胜感激。

最后来自 XAML 的 sn-p 与 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);
        }
    }

【问题讨论】:

  • 您是否使用 MVVM 模式绑定到您的 DataGrid?如果是这样,我可以建议将 SelectedItem 属性绑定到模型中的属性 - 您可以将其设置为 null 并且它应该取消选择网格中的项目。
  • @Johan:你好,约翰。是的,我正在使用 MVVM,但由于 SelectionUnit 单元,我无法使用 SelectedItem。 SelectedItem 仅适用于 SelectionUnit Row。是不是我错了?
  • 哦,我明白了,行选择不是问题,而是单个单元格?您使用的是哪个 DataGrid?
  • @Johan:我正在使用 Visual Studio 2015 的标准 DataGrid,.NET Framework 4.5.2。
  • 您介意将 Datagrid 的 xaml 添加到您的问题中吗?

标签: c# wpf datagrid


【解决方案1】:

我已经设置了一个能够清除 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>

注意,我通过 CommandParameter 将 Datagrid 传递给按钮命令

数据上下文

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 事件导致调用设置器。我是否有另一种可能在运行时检测所选单元格的行和列索引?
  • 你好。我删除了 CurrentCellChanged 事件,并将用于获取行和列的代码转移到了两个属性的 getter 中,通过这些属性我无法在其他方法中获取它们。 CurrentCell 的 Setter 现在只在我启动窗口时调用一次。但是当我向上或向下移动一行时,最后一个选定的单元格似乎仍处于选中状态。
【解决方案2】:

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

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

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

【讨论】:

  • 很高兴能帮上忙!
猜你喜欢
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 2015-04-12
  • 2020-12-14
  • 1970-01-01
相关资源
最近更新 更多