C# 如何将非依赖性文本属性绑定到Application.Current.Resources?

C# 如何将非依赖性文本属性绑定到Application.Current.Resources?,c#,wpf,binding,C#,Wpf,Binding,如何使用XAML将非依赖性文本属性绑定到Application.Current.Resources 我想在第三方dll中使用具有非依赖性文本属性的控件,并且我想将Application.Current.Resources绑定到该属性 它不能使用DynamicSource扩展,因为它是非依赖属性 我该怎么办?假设您只想在第三方控件的Text属性中显示Resources值,您可以将第三方控件的Text属性包装在WPF中,并对其绑定/使用DynamicSource public static read

如何使用XAML将非依赖性文本属性绑定到Application.Current.Resources

我想在第三方dll中使用具有非依赖性文本属性的控件,并且我想将Application.Current.Resources绑定到该属性

它不能使用DynamicSource扩展,因为它是非依赖属性


我该怎么办?

假设您只想在第三方控件的Text属性中显示Resources值,您可以将第三方控件的Text属性包装在WPF中,并对其绑定/使用DynamicSource

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;
}

如何将AttachedProperty注册到多个控件?因为在第三方dll中有许多具有文本属性的控件。我可以为它们创建注册AttachedProperty的方法吗?@Noppol是由第三方库中的公共基控件类型声明的文本属性?如果是,请在附加的属性定义中指定此公共基类型。文本属性不是由基控件声明的。将属性附加到控件的最佳方式是什么?因此,我无法将obj映射到ThidPartyControl(var thirdParty=obj as ThirdPartyControl;)。它不能将DependencyObject大小写为my type。定义Text属性的类型是什么?这是您应该指定为
DependencyProperty.RegisterAttached
调用的第三个参数的类型,也是您应该将
TextWrappedChanged
的obj参数强制转换为的类型。我刚刚使用了
ThirdPartyControl
作为一个例子,它应该被替换为您需要的类型。我认为这个主题应该叫做“如何将非依赖性文本属性绑定到Application.Current.Resources”,这更清楚地说明了您真正需要做什么