【问题标题】:Create properties that only apply on design time创建仅适用于设计时的属性
【发布时间】:2015-12-28 14:24:49
【问题描述】:

我正在使用 Visual Studio 深色主题。结果,在设计我的视图时,如果字体是黑色的,我就看不到字体。解决方法是将视图的背景设置为白色。但是我们的应用程序有不同的主题,所以我不能硬编码。

我在创建用户控件时使用了很多很棒的属性:

d:DesignWidth="1110" d:DesignHeight="400"

这些属性只在设计时影响视图。如果我可以创建一个属性d:DesignBackground 那就太好了,这样我就不必在每次运行应用程序时添加和删除背景属性。

【问题讨论】:

  • Foreground 是由 DynamicResource 设置的吗?
  • 是的背景,但在主窗口。我正在处理用户控件,它没有背景。
  • 那么根据你的主题,你如何设置 FontColor ?
  • 一个选项是更改苹果酒 - ArtboardBackground,看看这里:wrightfully.com/…。我必须检查编辑器上的“显示所有元素”按钮。

标签: wpf xaml designmode


【解决方案1】:

不确定它是否正是您要查找的内容,但我所做的只是在 app.xaml 中添加一个触发器,以使用 IsInDesignMode 之类的属性进行调用;

命名空间(感谢 Tono Nam);

xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=PresentationFramework"

XAML;

<Style TargetType="{x:Type UserControl}">
    <Style.Triggers>
        <Trigger Property="ComponentModel:DesignerProperties.IsInDesignMode"
                 Value="True">
            <Setter Property="Background"
                    Value="#FFFFFF" />
        </Trigger>
    </Style.Triggers>
</Style>

简单,但有效,有时我也会根据需要定位其他依赖属性,如字体和东西。希望这会有所帮助。

PS - 您可以以相同的方式使用自己的属性定位其他 TargetType,例如 ChildWindows、Popups、Windows 等等......

【讨论】:

  • 这可用于在设计模式下设置任何样式!喜欢你的回答谢谢!
  • 如果有人没有 resharper 并且需要知道 ComponentModel 的命名空间,这里是:xmlns:componentModel="clr-namespace:System.ComponentModel;assembly=PresentationFramework"
  • 对于遇到奇怪 this namespace is not a valid xaml 错误的人 - 首先,删除命名空间声明和样式,然后像上面一样粘贴命名空间声明,删除 ; as‌​sembly=PresentationF‌​ramework 部分,然后添加样式并将 ;as‌​sembly=PresentationF‌​ramework 部分添加回命名空间。一种类似魔法的解决方案,但似乎解决了这个错误。
  • @erem 嗯,这很奇怪。很高兴您能分享一个解决方法!
【解决方案2】:

您可以为设计模式创建一个带有附加属性的静态类:

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Helpers.Wpf
{
    public static class DesignModeHelper
    {
        private static bool? inDesignMode;

        public static readonly DependencyProperty BackgroundProperty = DependencyProperty
            .RegisterAttached("Background", typeof (Brush), typeof (DesignModeHelper), new PropertyMetadata(BackgroundChanged));

        private static bool InDesignMode
        {
            get
            {
                if (inDesignMode == null)
                {
                    var prop = DesignerProperties.IsInDesignModeProperty;

                    inDesignMode = (bool) DependencyPropertyDescriptor
                        .FromProperty(prop, typeof (FrameworkElement))
                        .Metadata.DefaultValue;

                    if (!inDesignMode.GetValueOrDefault(false) && Process.GetCurrentProcess().ProcessName.StartsWith("devenv", StringComparison.Ordinal))
                        inDesignMode = true;
                }

                return inDesignMode.GetValueOrDefault(false);
            }
        }

        public static Brush GetBackground(DependencyObject dependencyObject)
        {
            return (Brush) dependencyObject.GetValue(BackgroundProperty);
        }

        public static void SetBackground(DependencyObject dependencyObject, Brush value)
        {
            dependencyObject.SetValue(BackgroundProperty, value);
        }

        private static void BackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!InDesignMode)
                return;

            d.SetValue(Control.BackgroundProperty, e.NewValue);
        }
    }
}

你可以这样使用它:

xmlns:wpf="clr-namespace:Helpers.Wpf;assembly=Helpers.Wpf"

<Grid Background="Black"
      wpf:DesignModeHelper.Background="White">
    <Button Content="Press me!"/>
</Grid>

您可以使用这种方法来实现设计模式的其他属性。

【讨论】:

    猜你喜欢
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    • 2020-05-12
    • 1970-01-01
    相关资源
    最近更新 更多