Creating properties that apply only at design time

I use the dark theme of visual studio. As a result, when designing my ideas, I do not see the font if it is black. The fix will be that the background will be white. But our application has different themes, so I can not do this.

There are great properties that I use when creating a usercontrol:

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

these properties only affect performance at design time. It will be great if I can create the property d:DesignBackgroundonly so that I do not have to add and remove the background property every time I start the application.

+6
source share
2 answers

, , , app.xaml IsInDesignMode like;

( );

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, ...

+12

:

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>

.

+2

Source: https://habr.com/ru/post/1609677/


All Articles