作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有白色文本前景色和透明背景色的控件。
稍后,这个用户控件将被添加到带有真实背景颜色的不同控件中。
但是在设计这个的过程中,在VS 2010中控制白色背景上的白色前景,我看不到任何东西。无论如何要为设计时间定义不同的颜色?
我试过这个:
if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
LayoutRoot.Background = new SolidColorBrush(Colors.Blue);
}
public MainPage()
{
InitializeComponent();
LayoutRoot.Background = new SolidColorBrush(Colors.Blue);
}
<UserControl x:Class="SilverlightApplication3.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot">
</Grid>
</UserControl>
最佳答案
这是一种方法:
if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
LayoutRoot.Background = new SolidColorBrush(Colors.Yellow);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Border b = this.GetTemplateChild("backBorder") as Border;
if (b != null && System.ComponentModel.DesignerProperties.IsInDesignTool)
{
b.Background = new SolidColorBrush(Colors.Orange);
}
}
<Style TargetType="local:TemplatedControl1">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TemplatedControl1">
<Border x:Name="backBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
#if DEBUG
if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
LayoutRoot.Background = new SolidColorBrush(Colors.Yellow);
}
#endif
UserControl
时才有效。您正在创建的*另一个*用户控件/控件在设计时使用。所以,如果我上面建议的代码放在
UserControl
中命名
UserControlWithDesignMode
,那么你必须有另一个
UserControl
,
UserControlHost
,其中包含
UserControlWithDesignMode
的实例控制在设计时查看行为工作。当前编辑的控件的代码隐藏在您编辑时不会执行。它仅在包含在另一个主机中时执行(例如在 Silverlight 中,另一个
UserControl
)。
关于visual-studio - XAML : How to change background color only in Design mode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4843276/
我是一名优秀的程序员,十分优秀!