¿Color de fondo solo en tiempo de diseño en WPF?

In WPF XAML there is the convenient DesignHeight y la DesignWidth, for instance in code as

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

which is great because I can build the layout with a representante, pero not locked-in, control size.

However, I'm often building dark UIs, where labels and so forth need to be white, but my controls still need a transparent background color. This creates a design-time inconvenience because white seems to be the default background color for transparent controls in the designer, leading to unreadable white-on-white labels.

Is there a way or strategy for setting the design-time background color, with similar convenience as DesignHeight/DesignWidth?

preguntado el 31 de enero de 12 a las 08:01

I'm not sure, by googling I found two documentation aquí y la aquí. May be they will be helpfull -

5 Respuestas

There's an undocumented property d:DesignStyle De tipo Style that you can set on a user control. This style is only applied in the designer and is not used at runtime.

Lo usas así:

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

O así:

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

Note however that any value set on the Style property (the one used at runtime) will also override the DesignStyle en el diseñador.

respondido 29 nov., 16:16

This is really nice and way easier than the other answers. - gartenriese

What is a design style and how to initialize it? To make this easy how do you set d:DesignStyle to White? - El hombre muffin

@TheMuffinMan In the second example code section above add <Setter Property="Background" Value="White" /> en el Style node to set the Background color to white. - Zorro Volador

Trying the second method in .NET Core WPF. I get wiggly line below Style saying "Object reference not set to an instance of object", and the background does not change. - Malditas verduras

I found that you can do one for yourself. Custom design-time attributes in Silverlight and WPF designer is a tutorial how to do it for both Silverlight and WPF.

Respondido 22 Jul 16, 18:07

This one is a bit nicer to me then the accepted answer, thanks. - Sevenate

This should be the accepted answer since it is much more modular and can be directly used in the designer without writing code for multiple controls. - Julian Sievers

My answer was found here: Fondo negro para el editor XAML. There are a number of choices including checking System.ComponentModel.DesignerProperties.GetIsInDesignMode(this) en tiempo de ejecución.

contestado el 23 de mayo de 17 a las 15:05

This is the complete solution for 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

    }

Uso:

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

Respondido 01 Feb 18, 10:02

El d:DesignerProperties.DesignStyle technique shown on this page works great for applying a WPF design-time-only style to a control único, but it doesn't appear to work for a Style en un parche de ResourceDictionary eso se aplicaría a todos of the appropriately-typed controls or elements under the scope of the dictionary. Below is simple solution I found for deploying a designer-only style into a ResourceDictionary.

Consider for example a Window que contiene un TreeView, where we want the TreeViewItem nodes to show as fully expanded—but only at design time. First, put the desired style in the XAML dictionary in the normal way.

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

Aquí el Style se pone en el ResourceDictionary de los Window but of course you could use any other subsuming dictionary instead. Next, in the C# code, eliminar el estilo de la Resource­Dict­ionary cuando el modo de diseño es no detectado. Haz esto en el OnInitialized anular:

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

    base.OnInitialized(e);
}

Modo de diseño: Modo de tiempo de ejecución:

modo de diseño    modo de ejecución

Respondido el 08 de diciembre de 17 a las 00:12

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.