XAML:如何仅在设计模式下更改背景颜色? [英] XAML : How to change background color only in Design mode?

查看:179
本文介绍了XAML:如何仅在设计模式下更改背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有白色文本前景色和透明背景色的控件. 稍后,此用户控件将被添加到带有真实背景色的其他控件中.

I have a control with white text foreground color and transparent background color. Later on this usercontrol will be added into a different control that carries the real background color.

但是在设计此控件期间,在VS 2010中控制白色背景上的白色前景,我显然看不到任何东西.反正在那里只为设计时间定义了不同的颜色?

However during designing this, control due white foreground on white background in VS 2010, I can't obviously see anything. In there anyway to define a different color for just the design time?

我已经尝试过了:

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

但这不起作用.有提示吗?

But this doesn't work. Any tips?

更新:

我不明白这对你们是怎么工作的.我创建了一个新的Silverlight 4.0应用程序,并将以下代码行插入到ctor中:

I dont understand how this works for you guys. I have created a new Silverlight 4.0 Application and have inserted this line of code into the ctor:

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条件.我在这里想念什么?

When I go into Designer, I still dont see it as blue. And I dont even have any isInDesignTime Condition there. What I am missing here?

谢谢, 冒犯

推荐答案

这是一种方法:

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

如果切换到创建模板控件,则需要等待在OnApplyTemplate中进行设置,如本例所示:

If you switch to creating a templated control, you'll need to wait to set things up in OnApplyTemplate, like in this example:

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>

我还喜欢围绕这样的代码添加条件编译指令,因为它仅适用于开发人员/设计人员,并且在运行时不需要.

I also like to add conditional compile directives around code like this, as it's only for the developer/designer and is never needed at run-time.

#if DEBUG
if (System.ComponentModel.DesignerProperties.IsInDesignTool)
{
    LayoutRoot.Background = new SolidColorBrush(Colors.Yellow);
}
#endif

请注意,仅当在设计时在* 另一个 * UserControl/Control中使用正在创建的UserControl时,整个技术才有效.因此,如果我上面建议的代码放在名为UserControlWithDesignModeUserControl中,则您必须具有另一个UserControlUserControlHost,其中包含UserControlWithDesignMode控件的实例,才能在设计时查看行为时间.当前编辑的控件的代码隐藏在您编辑时不会执行.仅当它包含在另一个主机(例如,在Silverlight中,另一个UserControl)中时才执行.

Note that this entire technique works only when the UserControl you're creating is used within* another* UserControl/Control at design time. So, if the code I suggested above is placed in a UserControl named UserControlWithDesignMode, then you must have another UserControl, UserControlHost, that contains an instance of the UserControlWithDesignMode control to see the behavior work at design time. The code-behind for the currently edited control does not execute when you're editing it. It only executes when it's contained within another host (in Silverlight, another UserControl for example).

这篇关于XAML:如何仅在设计模式下更改背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆