Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XAML syntax error is blocking designer but compiling fine

I've run into a rather bizarre thing happening, I have a DataGrid defined in a WPF XMAL page that has the following declared:

<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 the two Inactive SystemColors types are from .net 4.5, however I can compile the program when it's set to target .net 4, and these Inactive brushes work, but while it's set to target .net 4 loading the XMAL designer page in Visual Studio throws the error The member "InactiveSelectionHighlightTextBrushKey" is not recognized or is not accessible. and then blocks the designer view. But it still compiles and displays as defined above in the program.

This seems very inconsistent to say the least and I can’t tell if this is a Visual Studio 2012 issue or if it’s allowing the program to compile because my development computer has .net 4.5 installed and it’s just changing the target framework when it sees that something is using it (I highly doubt this though). Or is it possible that the Inactive types are in .net 4 but not listed as supported in the documentation and just causing this issue in VS?

Is there a better way to do this in .net 4 to allow me to set the inactive selection color of a DataGrid row? Or is the only way to do this being to upgrade to .net 4.5?

like image 425
siva.k Avatar asked Dec 10 '12 22:12

siva.k


2 Answers

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

like image 105
Mikhail Shcherbakov Avatar answered Nov 16 '22 02:11

Mikhail Shcherbakov


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>
like image 35
David Avatar answered Nov 16 '22 00:11

David