¿Hay una propiedad DesignMode en WPF?


En Winforms puedes decir

if ( DesignMode )
{
  // Do something that only happens on Design mode
}

¿Hay algo como esto en WPF?

Author: Russ, 2009-01-08

5 answers

De hecho hay :

Sistema.ComponentModel.Propiedades del diseñador.GetIsInDesignMode

Ejemplo:

using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;

public class MyUserControl : UserControl
{
    public MyUserControl()
    {
        if (DesignerProperties.GetIsInDesignMode(this))
        {
            // Design-mode specific functionality
        }
    }
}
 145
Author: Enrico Campidoglio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-11-18 16:05:10

En algunos casos necesito saber si el diseñador inicia una llamada a mi clase que no es UI (como si creara una clase DataContext desde XAML). Entonces el enfoque de este artículo de MSDN es útil:

// Check for design mode. 
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)) 
{
    //in Design mode
}
 46
Author: Max Galkin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-04 17:06:35

Para cualquier control WPF alojado en WinForms, DesignerProperties.GetIsInDesignMode(this) no funciona.

Entonces, creé un error en Microsoft Connect y agregué una solución:

public static bool IsInDesignMode()
{
    if ( System.Reflection.Assembly.GetExecutingAssembly().Location.Contains( "VisualStudio" ) )
    {
        return true;
    }
    return false;
}
 19
Author: serhio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-02 18:56:41

Respuesta tardía, lo sé-pero para cualquier otra persona que quiera usar esto en un DataTrigger, o en cualquier lugar en XAML en general:

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

<Style.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
                 Path=(componentModel:DesignerProperties.IsInDesignMode)}" 
                 Value="True">
        <Setter Property="Visibility" Value="Visible"/>
    </DataTrigger>
</Style.Triggers>
 3
Author: Manfred Radlwimmer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-30 14:34:21

Use este:

if (Windows.ApplicationModel.DesignMode.DesignModeEnabled)
{
    //design only code here
}

(Las operaciones asincrónicas y de archivos no funcionarán aquí)

También, para crear una instancia de un objeto en tiempo de diseño en XAML (d es el espacio de nombres del diseñador especial)

<Grid d:DataContext="{d:DesignInstance Type=local:MyViewModel, IsDesignTimeCreatable=True}">
...
</Grid>
 -1
Author: Jeson Martajaya,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-07 19:04:21