开发者

Attached property problem

开发者 https://www.devze.com 2023-01-08 15:14 出处:网络
I have a attached property which name is \"Translate\". I set the property like this: <LabelTarget=\"{Bi开发者_运维百科nding ElementName=UserName}\"

I have a attached property which name is "Translate". I set the property like this:

<Label  Target="{Bi开发者_运维百科nding ElementName=UserName}" 
        Content="User Name"
        Extensions.Translate="true"/>

I get the Target value in the property changed event handler and it is null. But I set it in the XAML. Why is it null?

Thanks.


Binding doesn't occur until later in the process of loading the UI so at the point that your local value of "true" is being applied the Binding has yet to be evaluated. You need to postpone the check of the Target value until after the Binding has been updated. This should get you started in the Translate PropertyChanged handler:

    Label label = dObj as Label;
    if (BindingOperations.IsDataBound(label, Label.TargetProperty))
    {
        Binding.AddTargetUpdatedHandler(label, (sender, args) =>
        {
            UIElement element = label.Target;
            // do something with element
        });
    }
0

精彩评论

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