我有一个具有白色文本前景色和透明背景色的控件。
稍后此用户控件将被添加到带有真实背景颜色的不同控件中。
然而在设计过程中,在VS 2010中控制白色背景上的白色前景,我明显看不到任何东西。无论如何,要在设计时定义不同的颜色吗?
我已经尝试过这个:
if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
LayoutRoot.Background = new SolidColorBrush(Colors.Blue);
}
但这行不通。有小费吗?
UPDATE:
我不明白这对你们来说有什么用。我创建了一个新的 Silverlight 4.0 应用程序,并将这行代码插入到构造函数中:
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>
当我进入 Designer 时,我仍然不认为它是蓝色的。我什至没有任何 isInDesignTime 条件。我在这里缺少什么?
谢谢,
卡韦
这是一种方法:
if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
LayoutRoot.Background = new SolidColorBrush(Colors.Yellow);
}
如果您切换到创建模板化控件,则需要等待在 OnApplyTemplate 中进行设置,如下例所示:
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
您正在创建的内容在*内使用another* 设计时的用户控制/控制。因此,如果我上面建议的代码放在UserControl
named UserControlWithDesignMode
,那么你必须有另一个UserControl
, UserControlHost
,其中包含一个实例UserControlWithDesignMode
控制以查看设计时的行为。当您编辑当前编辑的控件时,其隐藏代码不会执行。它仅在包含在另一个主机中时执行(在 Silverlight 中,另一个主机)UserControl
例如)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)