How can I do this in WPF's code-behind ?
<Grid Background="{DynamicResource {x:Static SystemColors.DesktopBrushKey}}"/>
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);
}
}