XAML syntax error blocks constructor but compiles fine

I came across a rather strange thing, I have a DataGrid defined on the WPF XMAL page that declared the following:

 <DataGrid.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF3399FF" /> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White" /> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#FF3399FF"/> <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}" Color="White" /> </DataGrid.Resources> 

Technically, two types of Inactive SystemColors are taken from .net 4.5, however I can compile the program when it is set to the target .net 4, and these inactive brushes work, but while it is configured to target .net 4 loading the XMAL designer page in Visual Studio gives an error The member "InactiveSelectionHighlightTextBrushKey" is not recognized or is not accessible. , and then blocks the constructor view. But it still compiles and displays as defined above in the program.

This, apparently, is very incompatible, and I can’t say if this is a Visual Studio 2012 problem or if it allows me to compile the program because my development computer has .net 4.5 and it just changes the target structure when it sees that something uses it (I really doubt it). Or is it possible that inactive types are in .net 4 but are not listed as supported in the documentation and just cause this problem in VS?

Is there a better way to do this in .net 4 so that I can set the inactive highlight color in the DataGrid row? Or is this the only way to do this to upgrade to .net 4.5?

+9
wpf visual-studio-2012 xaml datagrid
Dec 10
source share
2 answers

Visual Studio creates the assembly even if its target audience is installed in .NET FW 4.0 and you use InactiveSelectionHighlightBrushKey in the XAML code. This build will work correctly on 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 creates a user control using InactiveSelectionHighlightBrushKey .

Thus, you cannot use InactiveSelectionHighlightBrushKey in assemblies with a target set in FW 4.0, because they will not work on a system only with .NET FW 4.0.

To support FW 4.0 and FW 4.5, you can set the color of the selected row in the LostFocus/LostKeyboardFocus/GotFocus event handlers. See code sample https://stackoverflow.com/a/166268/

+15
Dec 11 '12 at 20:27
source share

If you don't want to use the code behind, 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> 
+9
Jan 21 '13 at 0:16
source share



All Articles