5

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

5
  • Answer to #2: it's much easier to make custom controls (using itemscontrol) and implement the custom logic you need.
    – Terry
    Jun 12, 2013 at 13:12
  • Is that also answer to question #3? Cause I need this only because I want to select other way then Ctrl + click on cell.. and also for selecting previously saved cells..
    – Ms. Nobody
    Jun 12, 2013 at 13:17
  • Of course, if you don't want ctrl-click to be used for multiselecting, you can make you own list and implement any way of multiselection you'd like. How do you want the users to multiselect?
    – Terry
    Jun 12, 2013 at 13:21
  • Well I want to single click on cell to be selected in mode expanded so I can have more selected cells not just one how is it possible now without Ctrl. And I also wanted to have maximum 7 cells selected. And if more than the first will be deselected. I tried to make this work with MouseLeftButtonDown event and almost solved it that way. Only had problem with deselecting cell that was already clicked on.
    – Ms. Nobody
    Jun 12, 2013 at 13:24
  • 1
    perfectly doable, let me build something fast!
    – Terry
    Jun 12, 2013 at 13:26

1 Answer 1

44

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;
    }
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.