WPF Datagrid单元格,cellinfo和selectedcells +自定义选择

时间:2013-06-12 13:06:08

标签: c# wpf datagrid selection cell

我想在WPF数据网格中操作选择,但是我在访问实际单元格时遇到问题,并且设置焦点并将它们标记为已选中。

  1. 任何人都可以解释一下:为什么没有一些简单的方法可以从** DatagridCellInfo **获取** DatagridCell **?
  2. 为什么几乎没有人在使用WPF数据网格? (我没有看到很多Q / A投票)
  3. 是否有一种简单的方法可以为WPF数据网格创建自己的选择模式?
  4. 我的问题是什么

    我想在不按Ctrl的情况下选择更多单元格(逐个)时在WPF Datagrid上进行自定义选择。我做得很好,但是当我想取消选择一个选定的单元格时,我遇到了问题 - 只需单击它即可。从列表中删除它不是问题。问题在于,当它被点击时它会聚焦并且被点亮,所有其他被选中的人都会关闭它们的暮色。如果我选择另一个未选中的单元格,则所有选定的单元格将再次正确显示。问题只出在取消选择中。

    我的代码:

    XAML:

    <Window x:Class="SelectionTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <Style TargetType="DataGridCell">
                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown"></EventSetter>
    
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Pink"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Window.Resources>
        <Grid>
            <DataGrid 
                Name="mydatagrid"
                Width="Auto" Height="Auto"
                HeadersVisibility="All"
                AutoGenerateColumns="True" IsReadOnly="True"
                SelectionMode="Extended" SelectionUnit="Cell" 
                CanUserAddRows="False" CanUserDeleteRows="False"
                CanUserResizeColumns="False" CanUserResizeRows="False" 
                CanUserReorderColumns="False" CanUserSortColumns="False"
                SelectedCellsChanged="mydatagrid_SelectedCellsChanged"
                Padding="10" HorizontalAlignment="Center" VerticalAlignment="Top"
                >            
            </DataGrid>  
        </Grid>
    </Window>
    

    我已经在datagrid中填充了我制作的一些随机示例类对象的列表。

    C#:

            private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                DataGridCell cell = sender as DataGridCell;
    
                DataGridCellInfo cellInfo = new DataGridCellInfo(cell);
    
                if ((cell.IsSelected)||(selectedList.Contains(cellInfo))||(selectedCellsList.Contains(cell)))
                {
                    selectedList.Remove(cellInfo);
                    selectedCellsList.Remove(cell);
                    cell.IsSelected = false;
                    mydatagrid.CurrentCell = selectedList[0];
                }
                else
                {
    
                   if (selectedList.Count < 7)
                   {
                       selectedList.Add(cellInfo);
                       selectedCellsList.Add(cell);
                   }
                   else
                   {
                      selectedList.RemoveAt(0);
                      selectedList.Add(cellInfo);
                      selectedCellsList.RemoveAt(0);
                      selectedCellsList.Add(cell);
                   }
                }
    
                mydatagrid.SelectedCells.Clear();
                mydatagrid.UnselectAll();
    
                foreach (DataGridCell xcell in selectedCellsList)
                {
                    xcell.IsSelected = true;
                    xcell.Focus();
                }
    }
    

    如果这段代码对你来说真的很难看,那我很抱歉。但我还只是一点点csharpawan。

    我在快捷方式中遇到的问题是什么:点击选定的单元格只会使其高亮显示并聚焦所有其他选定的单元格,这与我希望它完全相反。 (如果我点击其他未选中的单元格,它会按照我想要的方式工作。)

1 个答案:

答案 0 :(得分:28)

回答问题1:从DataGridCellInfo获取DataGridCell的快捷方法:

    public DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
    {
        var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
        if (cellContent != null)
            return (DataGridCell) cellContent.Parent;

        return null;
    }