Programing

WPF에 DesignMode 속성이 있습니까?

crosscheck 2020. 8. 19. 07:38
반응형

WPF에 DesignMode 속성이 있습니까?


Winforms에서는 다음과 같이 말할 수 있습니다.

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

WPF에 이와 같은 것이 있습니까?


실제로 다음이 있습니다 .

System.ComponentModel.DesignerProperties.GetIsInDesignMode

예:

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

public class MyUserControl : UserControl
{
    public MyUserControl()
    {
        if (DesignerProperties.GetIsInDesignMode(this))
        {
            // Design-mode specific functionality
        }
    }
}

어떤 경우에는 UI가 아닌 클래스에 대한 호출이 디자이너에 의해 시작되었는지 여부를 알아야합니다 (예 : XAML에서 DataContext 클래스를 만드는 경우). 이 MSDN 기사 의 접근 방식 이 도움이됩니다.

// Check for design mode. 
if ((bool)(DesignerProperties.IsInDesignModeProperty.GetMetadata(typeof(DependencyObject)).DefaultValue)) 
{
    //in Design mode
}

WinForms 에서 호스팅되는 WPF 컨트롤의 DesignerProperties.GetIsInDesignMode(this)경우 작동하지 않습니다.

그래서 Microsoft Connect에서 버그를 만들고 해결 방법을 추가했습니다.

public static bool IsInDesignMode()
{
    if ( System.Reflection.Assembly.GetExecutingAssembly().Location.Contains( "VisualStudio" ) )
    {
        return true;
    }
    return false;
}

늦은 대답은 알고 있지만 DataTrigger일반적으로 XAML 또는 XAML에서 이것을 사용하려는 다른 사람을 위해 :

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>

이것을 사용하십시오 :

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

(비동기 및 파일 작업은 여기서 작동하지 않습니다)

Also, to instantiate a design-time object in XAML (d is the special designer namespace)

<Grid d:DataContext="{d:DesignInstance Type=local:MyViewModel, IsDesignTimeCreatable=True}">
...
</Grid>

참고URL : https://stackoverflow.com/questions/425760/is-there-a-designmode-property-in-wpf

반응형