如何使用XAML将非依赖性Text属性绑定到Application.Current.Resources?如何将非依赖性Text属性绑定到Application.Current.Resources?
我想在第三方DLL中使用具有非依赖性Text属性的控件,并且我想将Application.Current.Resources绑定到该属性。
它不能使用DynamicResource扩展,因为它是非依赖项属性。
我该做什么?
如何使用XAML将非依赖性Text属性绑定到Application.Current.Resources?如何将非依赖性Text属性绑定到Application.Current.Resources?
我想在第三方DLL中使用具有非依赖性Text属性的控件,并且我想将Application.Current.Resources绑定到该属性。
它不能使用DynamicResource扩展,因为它是非依赖项属性。
我该做什么?
假设您只想显示第三方控件的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;
}
我觉得题目应该叫做“如何不依赖Text属性绑定到Application.Current.Resources”,这是什么,你真正需要做的 – 2011-05-03 06:21:43