如何使用XAML将非依赖Text属性绑定到Application.Current.Resources?
我想在第三方dll中使用Control,它具有非依赖性Text属性,我想将Application.Current.Resources绑定到该属性。
它不能使用DynamicResource扩展,因为它是非依赖属性。
我该怎么办?
答案 0 :(得分:0)
假设您只想在第三方控件的Text属性中显示Resources值,您可以将WPF attached property中的第三方控件的Text属性包装起来,然后绑定/使用DynamicResource。< / p>
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;
}