XAML编辑器的黑色背景

Col*_*inE 14 silverlight wpf visual-studio-2010

我目前正在开发一个具有白色文本和透明背景的用户控件.不幸的是因为VS2010中的XAML设计视图有白色背景我看不到任何我正在设计的东西!

我已经浏览了所有我能想到的设置对话框,但一直无法找到改变XAML设计器背景颜色的设置.

有谁知道如何做到这一点?

Joh*_*ner 26

或者,从VS 2013开始,您可以在工具 - >选项 - >字体和颜色,XAML UI设计器中执行此操作.

可编辑的前景/背景颜色有棋盘背景的颜色.我只是将它们都设置为深灰色,这似乎适用于浅色和深色主题背景材料.


Met*_*urf 17

在您的XAML中,将背景设置为黑色.然后在用户控件中,使用DesignerProperties在运行时设置背景:

XAML

<UserControl .... Background="Black" .... >
Run Code Online (Sandbox Code Playgroud)

代码背后

public YourUserControl()
{
  InitializeComponent();

  if( !System.ComponentModel.DesignerProperties.GetIsInDesignMode( this ) )
  {
    this.Background = Brushes.Transparent;
  }

}
Run Code Online (Sandbox Code Playgroud)



替代方法

用户控件:

在用户控件中,不要声明背景颜色:

<UserControl ... namespaces ...>
Run Code Online (Sandbox Code Playgroud)

UserControl代码背后:

在用户控件的构造函数中,使用上面的DesignTime方法,但检查它是否是设计模式(与其他方法相反):

public YourUserControl()
{
  InitializeComponent();

  if( System.ComponentModel.DesignerProperties.GetIsInDesignMode( this ) )
  {
    this.Background = Brushes.Black;
  }

}
Run Code Online (Sandbox Code Playgroud)

App.xaml中:

最后,在App.xaml中,添加样式以设置UserControls的背景颜色:

<Application.Resources>
  <Style TargetType="{x:Type UserControl}">
    <Setter Property="Background" Value="Black" />
  </Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

这是发生了什么:

  1. App.xaml将在设计时影响UserControl,因为类型样式会自动应用于对象,但它不会应用于派生对象(在本例中为UserControl).因此,在设计时,VS认为它应该应用样式,但在运行时,它将被忽略.
  2. GetIsInDesignMode在使用UserControl的Window中查看控件时,检查将影响UserControl,因为VS正在设计时编译UserControl以便在可视设计器中呈现它.

HTH的

  • 一个小的改进可能是设置清除Background属性而不是将其设置为Transparent,除非你通过在构造函数中调用ClearValue(BackgroundProperty)来实际上想要Transparent而不是Null.请注意,这两个值在鼠标处理方面有所不同,并且默认情况下,对于UserControl,Background为null. (2认同)

nmc*_*ean 5

如本文所示,您可以使用触发器将代码压缩为单一样式,因为DesignerProperties.IsInDesignMode附加属性

实际上,那里的代码不太正确。它为 定义了一个隐式样式TargetType="{x:Type UserControl}",无论如何,它在运行时都会被忽略,因为您的 UserControl 实际上是一个派生类 - 正如Metro Smurf在他的第一点中指出的那样:

App.xaml 将在设计时影响 UserControl,因为类型化样式会自动应用于对象,但不会应用于派生对象(在本例中为 UserControl)。因此,在设计时,VS认为应该应用该样式,但在运行时,它会被忽略。

正确的方法是给它一个密钥并将其手动应用到您的用户控件:

<Application
    ...
    xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=PresentationFramework">
    ...
    <Application.Resources>
        ...
        <Style x:Key="DesignerBlackBackgroundStyle" TargetType="Control">
            <Style.Triggers>
                <Trigger Property="componentModel:DesignerProperties.IsInDesignMode"
                         Value="True">
                    <Setter Property="Background" Value="Black" />
                </Trigger>
            </Style.Triggers>
        </Style>
Run Code Online (Sandbox Code Playgroud)

和:

<UserControl x:Class="MyUserControl"
             Style="{StaticResource ResourceKey=DesignerBlackBackgroundStyle}">
Run Code Online (Sandbox Code Playgroud)

作为触发器,这比在代码隐藏中设置背景有一个额外的好处——如果背景在其他地方显式设置,例如从包含的 UserControl 中设置,它将正常运行:

<UserControl x:Class="ContainerUserControl" ...>
    ...
    <local:MyUserControl Background="Gray" />
Run Code Online (Sandbox Code Playgroud)

局部值优先于样式触发器MyUserControl,因此在此屏幕上设计者将使用灰色背景,而在独立设计时则为黑色。