35

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

<Grid Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Jeno Csupor
  • 2,869
  • 6
  • 30
  • 35
  • 5
    Thanks for posting this question. I'm still learning WPF and I couldn't figure out how to do this in XAML, but your question shows exactly how to do it. I used this instead: Thanks! – Pretzel Mar 09 '10 at 14:11
  • 13
    FWIW you don't need to define the DynamicResource, you can access the static brush directly: Background="{x:Static SystemColors.ControlBrush}" – sourcenouveau Apr 20 '10 at 20:20
  • 5
    I revise my earlier comment: You do need to specify a DynamicResource if you think your users will change the system theme while you application is running. If you just use the {x:Static ... } and the theme changes, your application colors won't change to match. Also DynamicResource is necessary when using a Style's Setter, e.g. . – sourcenouveau Nov 16 '10 at 16:21

4 Answers4

15

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).

Jeno Csupor
  • 2,869
  • 6
  • 30
  • 35
9

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}}"/>

jt000
  • 3,196
  • 1
  • 18
  • 36
6

Extension methods might help:

public static class FrameworkElementExtensions
{
    // usage xPanel.SetBackground(SystemColors.DesktopBrushKey);
    public static void SetBackground(this Panel panel, ResourceKey key)
    {
        panel.SetResourceReference(Panel.BackgroundProperty, key);
    }

    // usage xControl.SetBackground(SystemColors.DesktopBrushKey);
    public static void SetBackground(this Control control, ResourceKey key)
    {
        control.SetResourceReference(Control.BackgroundProperty, key);
    }
}
orcun
  • 14,969
  • 1
  • 22
  • 25
  • this can be changed to the following, because all of thies are of type FrameworkElement:
    `public static class FrameworkElementExtensions { // usage xControl.SetBackground(SystemColors.DesktopBrushKey); public static void SetBackground(this FrameworkElement control, ResourceKey key) { control.SetResourceReference(Control.BackgroundProperty, key); } }`
    – Meister Schnitzel Oct 28 '15 at 14:47
3

.NET Framework Supported in: 3.0

https://msdn.microsoft.com/en-us/library/system.windows.systemcolors.highlightbrush(v=vs.85).aspx https://msdn.microsoft.com/en_us/library/system.windows.systemcolors.highlightbrushkey(v=vs.85).aspx

this.background=SystemColors.HighlightBrush;
Nightly
  • 31
  • 5
  • 1
    Note that this won't get updated when the System Color changes like when the user switches to Hi-Contrast mode unless you listen for change events and reset it manually. – jt000 Mar 29 '15 at 15:02
  • ```SystemColors.GrayTextBrush``` is what I was looking for – Flou May 23 '22 at 11:13