जवाबों:
वास्तव में है :
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
}
}
}
Enable project codeसक्षम होना चाहिए (या मेनू-> डिज़ाइन->) रन प्रोजेक्ट कोड)।
कुछ मामलों में मुझे यह जानने की जरूरत है कि क्या मेरे गैर-यूआई वर्ग के लिए एक कॉल डिजाइनर द्वारा शुरू किया गया है (जैसे कि अगर मैं एक्सएएमएल से डेटाकोटेक्स्ट क्लास बनाता हूं)। फिर इस 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;
}
GetEntryAssembly()इसके बजाय नहीं होना चाहिए GetExecutingAssembly()? उत्तरार्द्ध को विधानसभा में लौटना चाहिए जहां यह संपत्ति परिभाषित की गई है
देर से जवाब, मुझे पता है - लेकिन किसी और के लिए जो इस का उपयोग करना चाहता है DataTrigger, या सामान्य रूप से एक्सएएमएल में कहीं भी:
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
}
(Async और फ़ाइल संचालन यहाँ काम नहीं करेंगे)
इसके अलावा, XAML में एक डिज़ाइन-टाइम ऑब्जेक्ट को इंस्टेंट करने के लिए (विशेष डिजाइनर नाम स्थान है)
<Grid d:DataContext="{d:DesignInstance Type=local:MyViewModel, IsDesignTimeCreatable=True}">
...
</Grid>
Windows.ApplicationModel) स्टोर एप्स के लिए है, जो विंडोज रनटाइम एपीआई में शामिल है। यदि आप सिर्फ नियमित रूप से विंडोज डेस्कटॉप एप्लिकेशन पर काम कर रहे हैं तो यह एक आउट-ऑफ-द-बॉक्स WPF समाधान नहीं है।