Michael Heisler
Michael Heisler

Reputation: 11

c# wpf SystemColors doesn't work correctly

I'm trying to color my Window with systemcolors but when I do it like this:

<Menu
    Width="Auto"
    Height="20"
    Background="{DynamicResource {x:Static SystemColors.MenuBarColor}}"/>

I'm getting a 8-digit hexnumber. My program does only accept 3 or 6 numbers for colors. Executing the program always gives an error message

System.Windows.Markup.XamlParseException: Zeilennummer "11" und Zeilenposition "4" 
von "Beim Festlegen der Eigenschaft" System.Windows.Controls.Panel.Background 
"wurde eine Ausnahme ausgelöst.". ---> System.ArgumentException: "#FFF0F0F0" ist 
kein gültiger Wert für die Eigenschaft "Background".

if I use "MenuBarColorKey" instead this will not be accepted as vaild value either. But then the text reads ' "MenuBarColorKey" is not a valid value....'

How can I correct this?

Thanks for your help

Michael

edit: @AwkwardCoder

Sorry, reading already a few years, I am new to writing in stackoverflow, so I can't comment or vote.

You are right. With brush it works. Thank You Very Much!

But I referred for the names to the utility from Zack Peterson as shown in

"wpf - How do I use the correct Windows system colors?"

and there I didn't find the extension "Brush". And this utility works for me.

Upvotes: 1

Views: 1506

Answers (2)

dkozl
dkozl

Reputation: 33394

There are 2 problems here. First you want to use Color for a Brush property and second you try to use Color as a resource key.

If you want to use it as a resource you need to use SystemColors.MenuBarBrushKey

Background="{DynamicResource {x:Static SystemColors.MenuBarBrushKey}}"

or you can use it directly and then you should use SystemColors.MenuBarBrush

Background="{x:Static SystemColors.MenuBarBrush}"

You'll find that SystemColors gives you 4 static properties for each colour

  • XxxxColor gives you Color structure
  • XxxxColorKey gives you ResourceKey to Color structure
  • XxxxBrush gives you SolidColorBrush class
  • XxxxBrushKey gives you ResourceKey to SolidColorBrush class

Upvotes: 4

AwkwardCoder
AwkwardCoder

Reputation: 25651

According to the MSDN page there is NOT a MenuBarColor, there is a MenuBarBrush

Upvotes: 0

Related Questions