gpt4 book ai didi

visual-studio - XAML : How to change background color only in Design mode?

转载 作者:行者123 更新时间:2023-12-04 12:24:45 24 4
gpt4 key购买 nike

我有一个带有白色文本前景色和透明背景色的控件。
稍后,这个用户控件将被添加到带有真实背景颜色的不同控件中。

但是在设计这个的过程中,在VS 2010中控制白色背景上的白色前景,我看不到任何东西。无论如何要为设计时间定义不同的颜色?

我试过这个:

if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
LayoutRoot.Background = new SolidColorBrush(Colors.Blue);
}

但这不起作用。有小费吗?

更新:

我不明白这对你们有什么作用。我创建了一个新的 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 时才有效。您正在创建的*另一个*用户控件/控件在设计时使用。所以,如果我上面建议的代码放在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com