开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜