2011-05-03 129 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