0

I'm pretty new to WPF so be kind...

I am placing a number of child controls into a DataGrid cell, however when I select (click on) one of the child controls the containing cell shows the blue selection highlight. Is there a way to change this functionality so it just selects the control that was clicked on.

I expect I could get around this by changing the selection colour to transparent or something, but the selection does more change the appearance, if the user then hits the ArrowDown key I want the selection to move from the control I consider to be selected not the cell that DataGrid thinks is selected.

Ideally I want to manage all the selection and navigation within the DataGrid.

enter image description here

In the example I would the control containing Neil9232 to be selected

Sprotty
  • 5,676
  • 3
  • 33
  • 52

1 Answers1

1

In your CellTemplate, set Focusable to false. It will stop snatching your clicks. For example, if you don't currently have a Style set, this would be the bare minimum:

 <DataGrid>
        <DataGrid.Resources>
            <Style x:Key="NotFocusable" TargetType="{x:Type DataGridCell}">
                <Setter Property="Focusable" Value="False"/>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource NotFocusable}"/>
                </DataGridTemplateColumn.CellStyle>                    
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
NWoodsman
  • 423
  • 4
  • 10
  • Great, that's killed off pretty much all the data grids built in mouse and keyboard interaction. Ill have a go at building that functionality back using my own handlers. I'm starting to think the dataGrid is not entirely the right control for this, I need the basic grid layout and the row virtualisation, any suggestions before I push on? – Sprotty Feb 23 '17 at 18:27
  • You could use a handler to focus to an inner child control on double-clicking the cell, for example. Or use a handler to handle selecteditem changes and focus to an inner control. Or you could use `IEditableObject.BeginEdit`, trigger a property change in your viewmodel, and bind a trigger to set focusable. – NWoodsman Feb 23 '17 at 19:15