微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

wpf – XAML编辑器的黑色背景

我目前正在使用具有白色文本和透明背景的用户控件.不幸的是,由于VS2010中的XAML设计视图具有白色背景,所以我无法看到我正在设计的任何内容

我已经通过了我可以想到的所有设置对话框,但是一直无法找到改变XAML设计器背景颜色的设置.

有人知道如何做到这一点吗?

解决方法

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

XAML

<UserControl .... Background="Black" .... >

代码背后

public YourUserControl()
{
  InitializeComponent();

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

}

替代方法

用户控件:

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

<UserControl ... namespaces ...>

UserControl代码背后:

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

public YourUserControl()
{
  InitializeComponent();

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

}

App.xaml中:

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

<Application.Resources>
  <Style targettype="{x:Type UserControl}">
    <Setter Property="Background" Value="Black" />
  </Style>
</Application.Resources>

发生了什么:

> App.xaml将在设计时影响UserControl,因为类型化样式会自动应用于对象,但不适用于派生对象(在这种情况下为UserControl).所以,在设计时,VS认为应该应用风格,但是在运行时它会被忽略.
> GetIsInDesignMode检查将在使用UserControl的窗口中查看控件时影响UserControl,因为VS正在设计时编译UserControl,以便在Visual Designer中呈现它.

HTH的

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐