2012-07-16 41 views
4

我創建跑了一個問題,我想要將控件綁定到windowsFormsHost控件。但衆所周知,屬性不是DP,所以我創建了一個包裝。創建可綁定WindowsFormsHost,但子級更新沒有反映到控制

/// <summary> 
    ///  Bindable version of windows form hosts 
    /// </summary> 
    public class BindableWindowsFormsHost : WindowsFormsHost 
    { 
     /// <summary> 
     /// Max value of the textbox 
     /// </summary> 
     public Control BindableChild 
     { 
      get { return (Control)GetValue(BindableChildProperty); } 
      set 
      { 
       SetValue(BindableChildProperty, value); 
      } 
     } 

     // Using a DependencyProperty as the backing store for Max. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty BindableChildProperty = 
      DependencyProperty.Register("BindableChild", typeof(Control), typeof(BindableWindowsFormsHost), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnBindableChildChanged))); 

     /// <summary> 
     /// Handles changes to the FlyoutWindowSize property. 
     /// </summary> 
     private static void OnBindableChildChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      ((WindowsFormsHost)d).Child = e.NewValue as Control; 
     } 
    } 

的e.NewValue得到我想要的控制,並將其設置正確,但我沒有看到變化被反射。孩子已設置,但無法用新控件查看windowsFormsHost。

任何人有想法?

感謝和問候, Kev84

回答

7

而不是創造一個包裝的,你可以包裹WindowsFormsHost在ContentControl中,並通過綁定設置其內容屬性。這樣可以避免WindowsFormsHosts Child屬性不是依賴項屬性的問題。

像這樣的東西在XAML:

<ContentControl Content="{Binding MyWindowsFormsHost}" /> 

..和這個在你的後臺代碼:

​​
+0

,但如果我這樣做,我還是要在代碼中設置兒童。然後調用MychangeowsFormHost的propertychange。我也看到了這個解決方案,但我認爲它不如包裝解決方案。謝謝 – Kev84 2012-07-16 19:40:44

+0

好吧,我想明白了,謝謝,是的,顯然我是在錯誤的軌道上,我現在明白了,謝謝。 – Kev84 2012-07-18 14:38:21

+0

精彩!!作品:) – 2015-02-08 14:27:17

相關問題