【问题标题】:Design-time-only background color in WPF?WPF中仅设计时的背景颜色?
【发布时间】:2012-02-22 23:42:25
【问题描述】:

在 WPF XAML 中有方便的 DesignHeight 和 DesignWidth,例如在代码中

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

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

但是,我经常构建深色 UI,其中标签等需要为白色,但我的控件仍需要透明背景色。这会造成设计时的不便,因为白色似乎是设计器中透明控件的默认背景色,导致白底白字标签难以阅读。

是否有设置设计时背景颜色的方法或策略,与 DesignHeight/DesignWidth 一样方便?

【问题讨论】:

标签: wpf visual-studio-2010 designer


【解决方案1】:

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

你可以这样使用它:

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

或者像这样:

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

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

【讨论】:

  • 这真的很好,而且比其他答案更容易。
  • 什么是设计风格,如何初始化?为方便起见,如何将 d:DesignStyle 设置为白色?
  • @TheMuffinMan 在上面的第二个示例代码部分中,将&lt;Setter Property="Background" Value="White" /&gt; 添加到Style 节点以将背景颜色设置为白色。
  • 在 .NET Core WPF 中尝试第二种方法。我在Style 下方得到一条摆动线,说“对象引用未设置为对象的实例”,并且背景没有改变。
【解决方案2】:

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

【讨论】:

  • 这个对我来说比接受的答案好一点,谢谢。
  • 这应该是公认的答案,因为它更加模块化,可以直接在设计器中使用,而无需为多个控件编写代码。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    这是 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" />
    

    【讨论】:

      【解决方案5】:

      本页显示的d:DesignerProperties.DesignStyle 技术非常适合将WPF 仅设计时样式应用于单个控件,但它似乎不能为ResourceDictionary 中的Style 工作,该ResourceDictionary 将适用于字典范围内的所有 适当类型的控件或元素。下面是我找到的简单解决方案,用于将仅限设计器的样式部署到 ResourceDictionary。

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

      <Window.Resources>
          <Style TargetType="TreeViewItem">
              <Setter Property="IsExpanded" Value="True" />
          </Style>
      </Window.Resources>
      

      在这里,Style 被放在了Window 的ResourceDictionary 中,但当然您可以使用任何其他包含字典。接下来,在 C# 代码中,在 未 检测到设计模式时从 Resource­Dict­ionary 中删除样式。这样做是在OnInitialized 覆盖:

      protected override void OnInitialized(EventArgs e)
      {
          if (DesignerProperties.GetIsInDesignMode(this) == false)
              Resources.Remove(typeof(TreeViewItem));
      
          base.OnInitialized(e);
      }
      

      设计模式:                                        运行时模式:

        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-18
        • 1970-01-01
        • 2016-08-09
        • 2019-09-01
        • 1970-01-01
        • 2012-05-27
        相关资源
        最近更新 更多