How can I do this in WPF's code-behind?
<Grid Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>
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).
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); } }
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}}"/>
.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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With