2009-01-08 30 views
92

Trong Winforms bạn có thể nóiCó thuộc tính DesignMode trong WPF không?

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

là có một cái gì đó như thế này trong WPF?

+1

Lưu ý rằng GetIsInDesignMode bị [lỗi khổng lồ giống như tài sản designMode ] (http://stackoverflow.com/questions/34664/designmode-with-controls) –

Trả lời

139

Trên thực tế có:

System.ComponentModel.DesignerProperties.GetIsInDesignMode

Ví dụ:

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

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

Tôi áp dụng giải pháp của bạn trong ứng dụng của tôi nhưng nó không hoạt động. Tôi đã hỏi ở đây http://stackoverflow.com/questions/3987439/init-grid-row-height-doesnt-work. Nếu bạn muốn, hãy tham gia với chúng tôi và thảo luận. –

+3

mã này không hoạt động nếu WPF được lưu trữ trên WinForm. – serhio

+0

@serhio Cảm ơn bạn đã chỉ ra điều đó. Bạn có biết bất kỳ cách giải quyết nào không? Btw có vẻ như nó không hoạt động trong Silverlight hoặc: http://connect.microsoft.com/VisualStudio/feedback/details/371837/designerproperties-getisindesignmode-doesnt-work-with-silverlight-pages –

45

Trong một số trường hợp tôi cần phải biết, cho dù một cuộc gọi đến lớp phi UI của tôi được khởi xướng bởi các nhà thiết kế (như nếu tôi tạo một lớp DataContext từ XAML). Sau đó, cách tiếp cận from this MSDN article là hữu ích:

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

Tôi đã áp dụng giải pháp của bạn trong ứng dụng của tôi nhưng nó không hoạt động. Tôi đã hỏi ở đây http://stackoverflow.com/questions/3987439/init-grid-row-height-doesnt-work. Nếu bạn muốn, hãy tham gia với chúng tôi và thảo luận. –

19

Đối với bất kỳ WPF Controls tổ chức trong WinForms, DesignerProperties.GetIsInDesignMode(this) không hoạt động.

Vì vậy, tôi đã tạo a bug in Microsoft Connect và thêm một workaround:

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

Không phải là 'GetEntryAssembly()' thay vì 'GetExecutingAssembly()'? Sau này phải trả về assembly nơi thuộc tính này được định nghĩa – fjch1997

-1

Sử dụng này một:

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

(async và File hoạt động sẽ không làm việc ở đây)

Ngoài ra, để nhanh chóng một đối tượng thiết kế thời gian trong XAML (d là không gian tên thiết kế đặc biệt)

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

Lớp đó ('Windows.ApplicationModel') là dành cho các ứng dụng Store, được bao gồm trong Windows Runtime API. Đây không phải là giải pháp WPF ngoài hộp nếu bạn chỉ làm việc trên một ứng dụng Windows thông thường. – qJake

3

Cuối câu trả lời, tôi biết - nhưng đối với bất kỳ ai khác muốn sử dụng điều này trong một DataTrigger, hoặc bất cứ nơi nào trong XAML nói chung:

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> 
Các vấn đề liên quan