WPF DataGrid单元格,cellinfo和了selectedCells +定制选择 [英] WPF Datagrid cell, cellinfo and selectedcells + custom selection

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

问题描述

我想操纵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. 谁能给我解释一下:为什么没有一些简单的方法来获得**的** DatagridCell从** ** DatagridCellInfo


  2. 为什么与WPF DataGrid中SO的工作几乎没有人? (我看不出有多少票Q / A上)


  3. 有没有一种简单的方法如何使WPF您自己的选择模式数据网格?

  1. Can anyone explain me: Why there isn't some simple way to get the **DatagridCell** from **DatagridCellInfo**?
  2. Why is almost nobody on SO working with WPF datagrids? (I don't see much Q/A with votes up)
  3. Is there an easy way how to make your own selection mode for WPF datagrid?

我想不按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.

什么是我的问题,在快捷方式:点击选中的单元格仅使它高亮,重点突出,dehilights所有其它选定的单元是完全相反的我想要它做的事。 (如果我点击其他尚未选定单元格它的工作原理我想要的方式。)

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单元格,cellinfo和了selectedCells +定制选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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