2011-05-03 59 views
0

如何使用XAML將非依賴性Text屬性綁定到Application.Current.Resources?如何將非依賴性Text屬性綁定到Application.Current.Resources?

我想在第三方DLL中使用具有非依賴性Text屬性的控件,並且我想將Application.Current.Resources綁定到該屬性。

它不能使用DynamicResource擴展,因爲它是非依賴項屬性。

我該做什麼?

+0

我覺得題目應該叫做「如何不依賴Text屬性綁定到Application.Current.Resources」,這是什麼,你真正需要做的 – 2011-05-03 06:21:43

回答

0

假設您只想顯示第三方控件的Text屬性中的Resources值,可以將第三方控件的Text屬性包裝在WPF attached property中,並根據該屬性綁定/使用DynamicResource。

public static readonly DependencyProperty TextWrappedProperty = 
          DependencyProperty.RegisterAttached("TextWrapped", 
           typeof(string), typeof(ThirdPartyControl), 
           new PropertyMetadata(false, TextWrappedChanged)); 

public static void SetTextWrapped(DependencyObject obj, string wrapped) 
{ 
    obj.SetValue(TextWrappedProperty, wrapped); 
} 

public static string GetTextWrapped(DependencyObject obj) 
{ 
    return (string)obj.GetValue(TextWrappedProperty); 
} 

private static void TextWrappedChanged(DependencyObject obj, 
              DependencyPropertyChangedEventArgs e) 
{ 
    // here obj will be the third party control so cast to that type 
    var thirdParty = obj as ThirdPartyControl; 

    // and set the value of the non dependency text property 
    if (thirdParty != null) 
     thirdParty.Text = e.NewValue; 
} 
+0

如何註冊AttachedProperty到多個控件更加清楚了嗎?因爲在第三方DLL中有許多控件具有Text屬性。我可以爲他們創建一個Register AttachedProperty的方法嗎? – Noppol 2011-05-03 05:03:16

+0

@Noppol是由第三方庫中的公共基本控件類型聲明的Text屬性?如果是這樣,請在附加屬性定義中指定此公共基類型 – 2011-05-03 05:47:58

+0

Text屬性未由基本控件聲明。將財產附加到控件的最佳方式是什麼? – Noppol 2011-05-03 07:42:05