Custom order

What is the procedure for applying attached properties to an object? I think I should ignore this, but here is my scenario: I have an attached property to insert a virtual machine into the view, and then another attached property, which depends on the first. I'm trying to figure out what happens if the second one is configured before the first, but I can't get the error! those. the first (model) is always set to the second, regardless of the order in xaml. Who controls the assimilation order? Can i change it?

Now I am dealing with late assigmement by subscribing to the procprty change event:

DependencyPropertyDescriptor dd = DependencyPropertyDescriptor.FromProperty(FrameworkElement.DataContextProperty,depo.GetType());
            dd.AddValueChanged(depo, (s, a) =>
            {
                ChangeDatacontext(s as DependencyObject);
            }

and to simulate a problem, I manually configure a new data object for the object.

Thanks Felix

+5
source share
1 answer

I cannot directly answer this question because I never rely on which property is set before the other, but you can control things using a method that both attached properties use.

here is an example from my current code:

    public static readonly DependencyProperty RuleVMProperty =
        DependencyProperty.RegisterAttached("RuleVM", typeof(DocumentRuleViewModel), typeof(DocumentRuleViewModel), new UIPropertyMetadata(null, RuleVMChanged));

    public static void RuleVMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var el = GetRefid(sender);
        var vm = args.NewValue as DocumentRuleViewModel;
        if(vm==null)
            return;
        vm.SetDocumentFromRefid(sender, el);
    }

    public static readonly DependencyProperty RefidProperty =
        DependencyProperty.RegisterAttached("Refid", typeof(XmlElement), typeof(DocumentRuleViewModel), new UIPropertyMetadata(RefidChanged));

    public static void RefidChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var el = args.NewValue as XmlElement;
        var vm = GetRuleVM(sender);
        if (vm == null)
            return;
        vm.SetDocumentFromRefid(sender, el);
    }

    private void SetDocumentFromRefid(DependencyObject sender, XmlElement element)
    {
        ... // this is where the actual logic sits
    }

so essentially you have two modified handlers, and no matter what triggers the last one executes the logic, because it sees if the other property is null.

+2
source

All Articles