.NET Core WPF 用户控件集设计时背景

Dam*_*les 1 wpf wpf-controls .net-core visual-studio-2019

我尝试了这个现有答案中解释的两种方法,但它们都不起作用。如何设置背景颜色?

方法一

<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>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

方法二

<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>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

在出现这些错误之前,我已经按下 Run 来构建它。

tha*_*guy 6

这是 .NET Core 设计器的问题。在 .NET Framework 中,这两种方法都有效,但对于第一种方法,您必须使用DynamicResource,因为样式是在使用后声明的。

d:DesignStyle="{DynamicResource MyDesignStyle}"
Run Code Online (Sandbox Code Playgroud)

.NET Core 有一种解决方法,其中一个答案中也包含该解决方法。您必须声明一个类型来定义附加的依赖属性,以检查您是否正在运行设计模式并设置相应的属性。这只是Background属性的一个例子,但它也可以扩展为使用 a Style

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);
   }
}
Run Code Online (Sandbox Code Playgroud)

UserControl将以下行添加到您的标记中以启用设计模式背景。

local:DesignModeProperties.Background="White"
Run Code Online (Sandbox Code Playgroud)