10

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
siva.k
  • 1,344
  • 14
  • 24
  • What happens when you run your program on a machine without 4.5 installed? – Nzc Dec 11 '12 at 08:38
  • I wish I could test this but don't have a computer around without 4.5 on it. – siva.k Dec 11 '12 at 22:16
  • @Nzc - The app fails to run on a machine without 4.5 (just randomly ran into this issue in one of my apps and came across this SO question while trying to find a resolution). – Scott Apr 08 '13 at 21:15
  • 1
    @Scott: Well, just happens we ran into a similar 4.0/4.5 problem last week. In our case, a threading bug. We 'fixed' it by enforcing installation of 4.5 in the installer. Apparently there are changes in 4.5 that break when running with 4.0, but there is no check on 4.5 being installed apparently. – Nzc Apr 09 '13 at 07:49
  • I just ran into this problem in VS2010 where a DataGrid using an InactiveSelectionHighlightBrushKey solid color brush would compile successfully but crash when executed. 'Fixed' it by installing .Net v4.5 as Nzc suggested (+1'd his comment) – sharpguru Sep 18 '13 at 01:16

2 Answers2

17

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

Community
  • 1
  • 1
Mikhail Shcherbakov
  • 1,826
  • 16
  • 22
11

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>
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
David
  • 429
  • 3
  • 7
  • 2
    Not really an alternative since it colors control's background and not inactive selection. – Nikita B Apr 10 '13 at 06:12
  • ControlBrushKey sets every alternate row as well as the row that has lost focus. – David Feb 13 '14 at 00:34
  • 1
    Even worse, setting `ControlTextBrushKey` also changes the default text colours, for rows and column headers. That makes it pretty useless in fact as all text will disappear unless selected. // You also need to explicitly set DataGrid.Foreground to something like Black to fix that. – ygoe Mar 21 '14 at 12:09
  • Works for me. Thanks. – Francesco B. Oct 11 '18 at 07:25