Reputation: 2230
I have a ControlTemplate for a control (i.e. Cell in DataGrid) and I want to change the background of the Row if a cell is selected. I did not do this on Row because the SelectionUnit is set to cells.
Any ideas how I can change the background of the row if the cell is selected?
Upvotes: 2
Views: 156
Reputation: 132618
I would just use a Trigger based on IsKeyboardFocusWithin
. This means that anytime an object in that DataGridRow
has keyboard focus, the row will be highlighted.
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Background" Value="White" />
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
The alternative is handling the ClickEvent
and navigating up the Visual Tree to find the DataGridRow, and setting it's background color from there. If you choose this route, I have some VisualTreeHelpers that will allow you to easily find an object in WPF's visual tree.
var row = VisualTreeHelpers.FindParent<DataGridRow>(clickedDataGridCell);
Upvotes: 0
Reputation: 19294
the DataGridRow containing the DataGridCell is the ancestor of this DataGridCell in the visual tree. (you can find the ancestor using VisualTreeHelper.GetParent()) So handle the selectionChanged event, find the row and change its background (keeping track of the row and previous row background to restore it during the next SelectionChanged)
Upvotes: 1