3

Is there a way to manipulate to order in which Dependency Properties receive their values :

XAML :

         <Window   local:MyHelper.SomeAttachedProp="{Binding Something , Mode=OneWay}">
              <Grid>
                  <TextBlock Text="Let him go first" 
                             local:MyHelper.AnotherAttachedBooleanProp="True" />
              </Grid>
         </Window>

CS :

         public static class MyHelper 
         {
             propa .... 

             OnSomeAttachedPropChanged( .... )
             {
                 // I WANT TO GET HERE FIRST
             }

             propa ..... 

             OnAnotherAttachedBooleanPropChanged( .... )
             { 
                 // I WANT TO GET HERE SECOND 
             }                    
         }

Currently i'm reaching AnotherAttachedBooleanPropChanged before OnSomeAttachedPropChanged is there any way to control to order in which Dependency Properties are updated ?

Edit :

I just remembered/realized something , the DP with a direct assignment will get updated before the bound one .

eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • I assume from your question that you're creating a control/window with the Xaml you posted. In that case, you're asking to change the internal rendering logic of WPF, which seems unlikely to be possible. Can you not restructure your code to handle this elegantly? If you need advice for that, explaining what's happening in your two `PropertyChanged` methods would help. – Dan Puzey Mar 03 '14 at 15:51
  • 1
    similar question [Order that DependencyProperties bindings are evaluated?](http://stackoverflow.com/questions/9744366/order-that-dependencyproperties-bindings-are-evaluated) – Lukas Kubis Mar 03 '14 at 15:57

1 Answers1

1

I don't know of any direct way to do that. And i personally never had the desire to do so. When you only use the OnXYZChanged methods to register to events (which is imo the most common case) you could register to an event which better suits the order of your desired code execution. For example, in the OnXYZChanged register to Loaded event and handle that, the order of these should be afair from Root to child, and therefore in the correct order.

Another way would be to test which one was called second, and do your logic in there. Another idea would be to use the Dispatcher and do all the logic at a later time where its sure that everything is loaded.

And a final idea, You could traverse with the VisualTreeHelper from the child element in the OnXYZChanged to the underlying window and set the property on the window first and then handle the logic for the child.

@Your edit:

If you think about it, it makes sense. The visual tree must be created and everything must be chained before the Binding can traverse the tree to find the appropiate DataContext.

dowhilefor
  • 10,971
  • 3
  • 28
  • 45
  • thanks for your help Lukas Kubi's link above actually lead me to the same conclusion i always have , never really on the order of which DP's change their values. – eran otzap Mar 03 '14 at 17:54