WPF中仅限设计时的背景色?

时间:2012-01-31 08:21:04

标签: wpf visual-studio-2010 designer

在WPF XAML中有方便的DesignHeightDesignWidth,例如在代码中

<UserControl ... d:DesignHeight="500" d:DesignWidth="500" ... />

这很棒,因为我可以使用代表构建布局,但未锁定,控件大小。

但是,我经常构建深色UI,其中标签等需要为白色,但我的控件仍然需要透明的背景色。这会造成设计时的不便,因为白色似乎是设计师中透明控件的默认背景颜色,导致无法读取的白色白色标签。

是否有设置时间背景颜色的方法或策略,与DesignHeight / DesignWidth具有类似的便利性?

5 个答案:

答案 0 :(得分:28)

您可以在用户控件上设置d:DesignStyle类型的未记录属性Style。此样式仅应用于设计器,不在运行时使用。

你这样使用它:

<UserControl ... d:DesignStyle="{StaticResource MyDesignStyle}" />

或者像这样:

<UserControl ...>
    <d:DesignerProperties.DesignStyle>
        <Style TargetType="UserControl">...</Style>
    </d:DesignerProperties.DesignStyle>
</UserControl>

但请注意,Style属性(在运行时使用的属性)上设置的任何值也将覆盖设计器中的DesignStyle

答案 1 :(得分:9)

我发现你可以自己做一个。 Custom design-time attributes in Silverlight and WPF designer是如何为Silverlight和WPF执行此操作的教程。

答案 2 :(得分:8)

我的回答在这里找到:Black Background for XAML Editor。有许多选择,包括在运行时检查System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)

答案 3 :(得分:1)

此页面上显示的d:DesignerProperties.DesignStyle技术非常适合将 WPF 仅限设计时的样式应用于单一控件,但它不适用似乎适用于Style中的ResourceDictionary,适用于所有适当类型的控件或字典范围内的元素。下面是我发现的将设计师风格部署到ResourceDictionary

的简单解决方案

例如,考虑一个包含Window的{​​{1}},我们希望TreeView个节点显示为完全展开 - 但仅限于设计时。首先,以正常方式将所需的样式放在XAML字典中。

TreeViewItem

此处,<Window.Resources> <Style TargetType="TreeViewItem"> <Setter Property="IsExpanded" Value="True" /> </Style> </Window.Resources> 放在Style的{​​{1}}中,但您当然可以使用任何其他包含字典。接下来,在C#代码中,当设计模式检测到时,ResourceDictionary中删除样式。这是在Window覆盖:

Resource­Dict­ionary

设计模式:运行时模式:

design mode runtime mode

答案 4 :(得分:0)

这是DesignBackground的完整解决方案:

public class DesignTimeProperties : DependencyObject
    {
        private static readonly Type OwnerType = typeof(DesignTimeProperties);

        #region DesignBackground (attached property)

        public static Brush GetDesignBackground(DependencyObject obj)
        {
            return (Brush)obj.GetValue(DesignBackgroundProperty);
        }

        public static void SetDesignBackground(DependencyObject obj, Brush value)
        {
            obj.SetValue(DesignBackgroundProperty, value);
        }

        public static readonly DependencyProperty DesignBackgroundProperty =
            DependencyProperty.RegisterAttached(
                "DesignBackground",
                typeof (Brush),
                OwnerType,
                new FrameworkPropertyMetadata(Brushes.Transparent,
                    DesignBackgroundChangedCallback));

        public static void DesignBackgroundChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (IsInDesignMode)
            {
                var control = d as Control;
                var brush = e.NewValue as Brush;
                if (control != null && brush != null)
                {
                    control.Background = brush;
                }
            }
        }

        public static bool IsInDesignMode
        {
            get
            {
                return
                    ((bool)
                        DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof (DependencyObject)).DefaultValue);
            }
        }

        #endregion

    }

用法:

<UserControl ... infra:DesignTimeProperties.DesignBackground="Black" />