簡體   English   中英

如何確保行為中的綁定順序

[英]How to ensure binding order in 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));
}

這可以綁定到 TextBlock。

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

當 SomeProperty 更改時,需要執行一項取決於 SomeOtherProperty 的操作

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

問題是SomeOtherProperty是 null 時SomeProperty更改。 SomeOtherProperty 的綁定順序在 SomeProperty 之后。

我該如何解決這個問題?

沒有辦法做你想做的事。

相反,我建議為 SomeOtherProperty 添加一個更改處理程序。

然后,當任一屬性更改時,您可以檢查是否擁有運行 DoSomeThingWithSomeOtherProperty() 所需的所有部分。

請記住,沒有什么可以阻止代碼的其他部分在運行時更改 Some 或 SomeOther 的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM