views:

36

answers:

1

Hi,

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

<Label  Target="{Binding 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.

+1  A: 

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
        });
    }
John Bowen
very good answer, thank you.
frameworkninja