Change RowBackground or CellBackground when DataGrid gets or loses Focus

Jun 24, 2009 at 9:03 PM
Edited Jun 24, 2009 at 9:33 PM

I am trying to provide users feedback about which control has input focus.  Out of the box the DataGrid's RowBackground, for a selected row, is blue whether the control has focus or not.  We are using the MVVM pattern and would prefer not to use any code behind and do this all with templates and triggers.  We are also using Prism so controls are injected into different regions of our shell.

I have added the following textblocks with bindings to try and determine which property to use to create the trigger or template.  

<TextBlock Text="{Binding Path=IsFocused, ElementName=MainDataGrid, Mode=OneWay}" />
<TextBlock Text="{Binding Path=IsKeyboardFocused, ElementName=MainDataGrid, Mode=OneWay}" />
<TextBlock Text="{Binding Path=IsKeyboardFocusWithin, ElementName=MainDataGrid, Mode=OneWay}" />

The only one that fires reliably is "IsKeyboardFocusWithin" on the DataGrid but this requires the user to click in a cell.  IsFocused does not seem to change if you are setting focus to the datagrid from another control by clicking anywhere in the datagrid.

Any help getting my head wrapped around this to enable us to show different selected row(s) background color based on whether the datagrid has input focus or not would be greatly appreciated.


Jun 30, 2009 at 12:23 AM
Edited Jun 30, 2009 at 12:25 AM
Love answering my own questions.  This makes the datagrid behave like a ListView.  
When it has focus the selected row is blue.  When you loose focus the selected row is gray.  
When you edit a cell the cell goes white and text is black.  Hope this helps someone out there.   
		<my:DataGrid ItemsSource="{Binding Source={StaticResource sortedItems}}" 
                x:Name="dataGrid" >
    				<my:DataGridTextColumn Header="Date" Binding="{Binding Path=WorkOrderEditDateTime}" FontWeight="Bold" Width="Auto"/>
    				<my:DataGridTextColumn Header="Action" Binding="{Binding Path=ActionLabel, Mode=OneWay}" Width="*"/>
                    <Style TargetType="my:DataGridCell">
                        <Setter Property="Background" Value="Transparent" />
                        <Setter Property="BorderBrush" Value="Transparent" />
                        <Setter Property="BorderThickness" Value="1" />
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="IsKeyboardFocusWithin" Value="True"/>
                                <Setter Property="Background" Value="#FFFFFF" />
                                <Setter Property="Foreground" Value="#000000" />
                                <Setter Property="BorderBrush" Value="#FF0000" />
                    <Style TargetType="my:DataGridRow">
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="IsKeyboardFocusWithin" Value="True"/>
                                <Setter Property="Background" Value="#0000CC" />
                                <Setter Property="Foreground" Value="#FFFFFF" />
                                <Setter Property="BorderBrush" Value="#AB00FF" />
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="IsKeyboardFocusWithin" Value="False"/>
                                <Setter Property="Background" Value="#666666" />
                                <Setter Property="Foreground" Value="#FFB300" />
                                <Setter Property="BorderBrush" Value="#AB00FF" />
Nov 7, 2009 at 12:27 AM
Edited Nov 7, 2009 at 12:29 AM

My question is related to changing the DataCell's background; however the trigger is not from the property of the DataCell, but rather from a dependency property of the data object bound to the column.  How would this be achieved?  Maybe I'm missing something simple... any guesses would be appreciated.

Nov 9, 2009 at 5:07 PM

Found one solution:

Changing the background color is achievable through code by accessing the DataGridCell object for any cell.  I saw from the DataGridSample code, there is a Helper.GetCell(DataGrid dataGrid, int row, int column) that will return DataGridCell.


Feb 4, 2010 at 11:05 AM 


Answer is given here!  This helped me resolving the issue