Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datagrid cell, cellinfo and selectedcells + custom selection

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. 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?

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.

My code:

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

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.)

like image 297
Ms. Nobody Avatar asked Jun 12 '13 13:06

Ms. Nobody


1 Answers

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;
    }
like image 198
Richard E Avatar answered Nov 06 '22 16:11

Richard E