简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM