Sprotty
Sprotty

Reputation: 5973

WPF datagrid - how to stop the cell being selected when a child control is selected

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

Upvotes: 0

Views: 1486

Answers (1)

NWoodsman
NWoodsman

Reputation: 507

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>

Upvotes: 1

Related Questions