2010-11-05 142 views
1

我搜索DesignMode自定義WPF UserControl上的布爾值... 如何正確我推動它嗎?WPF UserControl DesignMode屬性(託管在WinForm上)

我有一個WinForm託管WPF控件。我看到了「DesignerProperties」類不會在這種情況下工作。

我在拋出異常的設計模式構造一些邏輯,想跳過的代碼,因爲我不到貨看到一個表格與我在設計用戶控件。

我試圖

private static bool? _isInDesignMode; 

/// <summary> 
/// Gets a value indicating whether the control is in design mode 
/// (running in Blend or Visual Studio). 
/// </summary> 
public static bool IsInDesignModeStatic 
{ 
    get 
    { 
     if (!_isInDesignMode.HasValue) 
     { 
#if SILVERLIGHT 
    _isInDesignMode = DesignerProperties.IsInDesignTool; 
#else 
      var prop = DesignerProperties.IsInDesignModeProperty; 
      _isInDesignMode 
       = (bool)DependencyPropertyDescriptor 
       .FromProperty(prop, typeof(FrameworkElement)) 
       .Metadata.DefaultValue; 
#endif 
     } 

     return _isInDesignMode.Value; 
    } 
} 

但這並不工作:((我看到設計師在例外 「封殺」 與IsInDesignModeStatic行代碼...

回答

1

試試這個

if (DesignerProperties.GetIsInDesignMode(this/*this user control*/)) 
    { 
     // Design-mode specific functionality 
    } 
+0

不起作用,設計師給了我一個錯誤堆棧跟蹤從裏面如果(!DesignerProperties ....) – serhio 2010-11-08 12:42:57

+0

我應該此話,我有一個WinForm和WPF控件裏面,這個代碼爲WPF控件無法正常工作,當我打開的Winform – serhio 2010-11-08 12:47:09

2

我用這種檢測的designMode(我的WPF控制是在一個類庫定義)。

' Exit here if in Design Mode 
    If Assembly.GetEntryAssembly() Is Nothing Then Exit Sub 

您可以檢查Assembly.GetEntryAssembly.FullName.ToString如果不是什麼並確定控制正在從初始化。

當控件託管在WinForms中時,DesignerProperties.IsInDesignModeProperty對我來說是返回null,因爲WPF不知道這裏有一個設計器。

史蒂夫