WPF Datagrid 单元格、单元格信息和选定单元格 + 自定义选择 [英] WPF Datagrid cell, cellinfo and selectedcells + custom selection

查看:72
本文介绍了WPF Datagrid 单元格、单元格信息和选定单元格 + 自定义选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to manipulate selection in WPF datagrid, but I have problem with access to actual cells and setting focus on them and marking them as selected.

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

我有什么问题

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

What's my problem

I wanted to make custom selection on WPF Datagrid when selecting more cells (one by one) without pressing Ctrl. I did it quite good but I'm having problems when I want to deselect one of selected cells - by just simply clicking on it. It's not a problem to remove it from the list. Problem is that When it's clicked on it takes focus and is hilighted and all others that were selected turn off their hilight. If I select another cell that wasn't selected all the selected cells get hilighted again correctly. The problem is only in the deselection.

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>

我已经用我制作的一些随机示例类对象的列表填充了数据网格.

I have filled the datagrid with list of some random example class objects I made.

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.

If this code looks really ugly to you, then I'm sorry. But I'm still just a little csharpawan.

我的快捷方式有什么问题:单击选定的单元格只会使其突出显示并聚焦,并取消突出显示所有其他选定的单元格,这与我想要它做的完全相反.(如果我点击其他尚未选择的单元格,它会按照我想要的方式工作.)

What's my problem in shortcut: Clicking on selected cell makes only it hilighted and focused and dehilights all other selected cells which is exact opposite what I want it to do. (If I click other not yet selected cell it works the way I want it.)

推荐答案

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

Answer to question 1: A quick way to get DataGridCell from DataGridCellInfo:

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

        return null;
    }

这篇关于WPF Datagrid 单元格、单元格信息和选定单元格 + 自定义选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆