I've played a bit here, nothing spectacular but is a working version:
<Style TargetType="DataGridCell">
<EventSetter Event="Selected" Handler="EventSetter_OnHandlerSelected"/>
<EventSetter Event="LostFocus" Handler="EventSetter_OnHandlerLostFocus"/>
</Style>
This is the codebehind:
private void EventSetter_OnHandlerSelected(object sender, RoutedEventArgs e)
{
DataGridRow dgr = FindParent<DataGridRow>(sender as DataGridCell);
dgr.Background = new SolidColorBrush(Colors.Red);
}
private void EventSetter_OnHandlerLostFocus(object sender, RoutedEventArgs e)
{
DataGridRow dgr = FindParent<DataGridRow>(sender as DataGridCell);
dgr.Background = new SolidColorBrush(Colors.White);
}
And this is the helper method to get the parent:
public static T FindParent<T>(DependencyObject child) where T : DependencyObject
{
//get parent item
DependencyObject parentObject = VisualTreeHelper.GetParent(child);
//we've reached the end of the tree
if (parentObject == null) return null;
//check if the parent matches the type we're looking for
T parent = parentObject as T;
if (parent != null)
return parent;
else
return FindParent<T>(parentObject);
}
It's not MVVM, but taking into consideration that we're just working with the View elements .. i think this time it's not a must.
So basically, on first selection you color the row, and on lost focus turn back to white the previous one and change color for the new selection.