I\'ve run into a rather bizarre thing happening, I have a DataGrid
defined in a WPF XMAL page that has the following declared:
If you don't want to use code behind then an alternative to InactiveSelectionHighlightBrushKey
is ControlBrushKey
. The following worked for me:
<Style x:Key="ReadOnlyDataGrid" TargetType="{x:Type DataGrid}">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGreen"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGreen"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black"/>
</Style.Resources>
</Style>
Visual Studio builds an assembly, even if its target set to .NET FW 4.0 and you use InactiveSelectionHighlightBrushKey
in XAML-code. This assembly will be correctly performed in a system with .NET FW 4.5. But if the system has only .NET FW 4.0, an exception will be thrown when the system create User control with InactiveSelectionHighlightBrushKey
.
So you cannot use the InactiveSelectionHighlightBrushKey
in assemblies with target set to FW 4.0, because they will not work in a system with only .NET FW 4.0.
To support both FW 4.0 and FW 4.5 you can set a color of the selected row in handlers of LostFocus/LostKeyboardFocus/GotFocus
events. See the example code https://stackoverflow.com/a/8095932/1815957