How can I set a WPF control's color to a system color programmatically, so that it updates on color scheme changes?


Question

How can I do this in WPF's code-behind?

<Grid Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>
1
33
4/10/2017 4:37:08 PM

Accepted Answer

I just found an ugly solution:

grid1.SetResourceReference(
    Control.BackgroundProperty,
    SystemColors.DesktopBrushKey);

I hope someone will post a better one (I'd like to see something like grid1.Background = BackgroundBrush, because the syntax of SetResourceReference is a step backwards from Windows Forms).

14
3/10/2009 12:12:31 AM

This must have been added to a later version of WPF since this was originally posted because your original code works fine for me (I'm using WPF 4.5)

<Grid Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon