How to bind non-dependency Text property to Application.Current.Resources?
How to bind non-dependency Text property to Application.Current.Resources using XAML?
I want to use the Control in third party dll which has non-dependency Text property and I want to bind the Application.Current.Resources to that property.
It canno开发者_开发技巧t use a DynamicResource extension because it is non-dependency property.
What shoud I do?
Assuming you just want to display the Resources value in the Text property of the third party control, you could wrap the Text property from the third party control in a WPF attached property and bind/use DynamicResource against that.
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;
}
精彩评论