0

I have a behavior

  public class MyBehaviour : Behavior<FrameworkElement>
 {

      public static readonly DependencyProperty SomeProperty =
            DependencyProperty.Register("Some", typeof(string), typeof(MyBehaviour), new UIPropertyMetadata(string.Empty, OnSomeChanged));

      public static readonly DependencyProperty SomeOtherProperty =
            DependencyProperty.Register("SomeOther", typeof(string), typeof(MyBehaviour), new UIPropertyMetadata(string.Empty));
}

This can be bound to a TextBlock.

<TextBox>
<i:interaction.Behaviors>
<ee:MyBehavior Some="{Binding Name}" SomeOther="{Binding OtherName}"/>
</i:interaction.Behaviors>
</TextBox>

When SomeProperty changes there is an action need to be done which depends on SomeOtherProperty

 private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
      DoSomeThingWithSomeOtherProperty(SomeOtherProperty );
}

The problem is SomeOtherProperty is null when SomeProperty is changed. The binding order for SomeOtherProperty is after SomeProperty.

How can I resolve this?

Jimmy
  • 3,224
  • 5
  • 29
  • 47
  • Readonly values can be set in the cosntructor (type constructor for statics), wich allwos you to enforce any order. That is their advantage of being Runtime Constants vs Compile Time Constants. – Christopher Sep 20 '19 at 15:46
  • How can I use the constructor in this scenario? – Jimmy Sep 20 '19 at 15:52

1 Answers1

1

There is no way to do what you want.

Instead, I would suggest adding a change handler for SomeOtherProperty.

Then, when either property changes you can check to see if you have all of the pieces you need to run DoSomeThingWithSomeOtherProperty().

Keep in mind that there is nothing stopping other parts of the code from changing the values of Some or SomeOther at runtime.

17 of 26
  • 27,121
  • 13
  • 66
  • 85
  • the only purpose of someother is to pass a value to the behavior. It is really an onetime value which is set in the viewmodel. I don't really like the change handler on that – Jimmy Sep 20 '19 at 16:23