首页 文章

如何使用SelectionUnit = Cell在WPF DataGrid中取消选择所选单元格

提问于
浏览
1

我在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);
        }
    }

2 回答

  • 1

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

    View

    <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传递给Button命令

    DataContext

    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 方法重新选择?

  • 0

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

    首先,我已经通过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();
    

相关问题