1

I tried the two methods explained in this existing answer, but neither of them is working. How can I set the background colour?

Method 1

<UserControl x:Class="deletewpf.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:deletewpf"
             mc:Ignorable="d" 
             d:DesignStyle="{StaticResource MyDesignStyle}"
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <Style TargetType="{x:Type Control}" x:Key="MyDesignStyle">
            <Setter Property="Background" Value="White"/>
        </Style>
    </UserControl.Resources>    
    <Grid>
            
    </Grid>
</UserControl>

enter image description here

Method 2

<UserControl x:Class="deletewpf.UserControl2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:deletewpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <d:DesignerProperties.DesignStyle>
        <Style TargetType="UserControl">
            <Setter Property="Background" Value="White"/>
        </Style>
    </d:DesignerProperties.DesignStyle>    
    <Grid>
            
    </Grid>
</UserControl>

enter image description here

I had pressed Run to build it before getting those errors.

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

1 Answers1

6

This is an issue with the .NET Core designer. In .NET Framework both methods work, but for the first method, you would have to use a DynamicResource, because the style is declared after it is used.

d:DesignStyle="{DynamicResource MyDesignStyle}"

There is a workaround for .NET Core, that is also included in one of the answers. You have to declare a type that defines attached dependency properties that check if you are running design mode and sets the corresponding properties. This is an example for the Background property only, but it can be extend for using a Style, too.

public class DesignModeProperties : DependencyObject
{
   public static readonly DependencyProperty BackgroundProperty = DependencyProperty.RegisterAttached(
      "BackgroundProperty", typeof(Brush), typeof(DesignModeProperties),
      new FrameworkPropertyMetadata(Brushes.Transparent, OnBackgroundChanged));

   public static void OnBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   {
      if (DesignerProperties.GetIsInDesignMode(d) && d is Control control && e.NewValue is Brush brush)
         control.Background = brush;
   }

   public static Brush GetBackground(DependencyObject dependencyObject)
   {
      return (Brush)dependencyObject.GetValue(BackgroundProperty);
   }

   public static void SetBackground(DependencyObject dependencyObject, Brush value)
   {
      dependencyObject.SetValue(BackgroundProperty, value);
   }
}

Add the following line to your UserControl markup to enbale the design-mode background.

local:DesignModeProperties.Background="White"
thatguy
  • 21,059
  • 6
  • 30
  • 40
  • The second method works, but how to get the first method working? How to define a "dynamic resource" MyDisignStyle? I have tried `Application.Current.Resources["MyDesignStyle"] = new SolidColorBrush(Colors.White);` as the existing answers for a dynamic resource example question, but that does not work. – Damn Vegetables Jul 27 '20 at 15:58
  • Sorry, maybe that was a misunderstanding. Both methods that you have proposed currently **do not** work with .NET Core designer. I meant that even for your first method **in .NET Framework** you need to use the `DynamicResource` markup extension instead of `StaticResource`, because otherwise there would be an error. By the way, using the dynamic markup extension only changes the way that the resource is _resolved_. You do not have to define resources differently for that. – thatguy Jul 27 '20 at 16:09
  • So, using `DesignModeProperties ` class is currently the only working way for .NET Core WPF? Anyway, I knew that .NET Core WinForms designer is currently problematic, but I did not know .NET Core WPF designer is so, too. And the new MAUI is not coming until next year... – Damn Vegetables Jul 27 '20 at 19:17
  • 1
    At least with the current version of Visual Studio 2019 none of the other XAML based approaches are working in the designer. – thatguy Jul 27 '20 at 19:19