如何:實作 PriorityBinding

PriorityBinding Windows Presentation Foundation (WPF) 的運作方式是指定繫結清單。 繫結清單會從最高到最低優先順序排序。 如果最高優先順序繫結在處理時成功傳回值,則永遠不需要處理清單中的其他繫結。 有可能最高優先順序繫結需要很長的時間評估,在較高優先順序的繫結成功傳回值之前,會使用成功傳回值的次高優先順序。

範例

為了示範 PriorityBinding 的運作方式,AsyncDataSource 物件已使用下列三個屬性建立:FastDPSlowerDPSlowestDP

FastDP 的 get 存取子會傳回 _fastDP 資料成員的值。

SlowerDP 的 get 存取子會等候 3 秒,再傳回 _slowerDP 資料成員的值。

SlowestDP 的 get 存取子會等候 5 秒,再傳回 _slowestDP 資料成員的值。

備註

此範例僅供示範之用。 .NET 指導方針建議不要定義比欄位集慢的量級順序的屬性。 如需詳細資訊,請參閱在屬性與方法之間選擇

public class AsyncDataSource
{
  private string _fastDP;
  private string _slowerDP;
  private string _slowestDP;

  public AsyncDataSource()
  {
  }

  public string FastDP
  {
    get { return _fastDP; }
    set { _fastDP = value; }
  }

  public string SlowerDP
  {
    get
    {
      // This simulates a lengthy time before the
      // data being bound to is actualy available.
      Thread.Sleep(3000);
      return _slowerDP;
    }
    set { _slowerDP = value; }
  }

  public string SlowestDP
  {
    get
    {
      // This simulates a lengthy time before the
      // data being bound to is actualy available.
      Thread.Sleep(5000);
      return _slowestDP;
    }
    set { _slowestDP = value; }
  }
}
Public Class AsyncDataSource
    ' Properties
    Public Property FastDP As String
        Get
            Return Me._fastDP
        End Get
        Set(ByVal value As String)
            Me._fastDP = value
        End Set
    End Property

    Public Property SlowerDP As String
        Get
            Thread.Sleep(3000)
            Return Me._slowerDP
        End Get
        Set(ByVal value As String)
            Me._slowerDP = value
        End Set
    End Property

    Public Property SlowestDP As String
        Get
            Thread.Sleep(5000)
            Return Me._slowestDP
        End Get
        Set(ByVal value As String)
            Me._slowestDP = value
        End Set
    End Property


    ' Fields
    Private _fastDP As String
    Private _slowerDP As String
    Private _slowestDP As String
End Class

Text 屬性會使用 AsyncDS 繫結至上述 PriorityBinding

<Window.Resources>
  <c:AsyncDataSource SlowestDP="Slowest Value" SlowerDP="Slower Value"
                     FastDP="Fast Value" x:Key="AsyncDS" />
</Window.Resources>
  
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"
  DataContext="{Binding Source={StaticResource AsyncDS}}">
  <TextBlock FontSize="18" FontWeight="Bold" Margin="10"
    HorizontalAlignment="Center">Priority Binding</TextBlock>
  <TextBlock Background="Honeydew" Width="100" HorizontalAlignment="Center">
    <TextBlock.Text>
      <PriorityBinding FallbackValue="defaultvalue">
        <Binding Path="SlowestDP" IsAsync="True"/>
        <Binding Path="SlowerDP" IsAsync="True"/>
        <Binding Path="FastDP" />
      </PriorityBinding>
    </TextBlock.Text>
  </TextBlock>	
</StackPanel>

當繫結引擎處理 Binding 物件時,它會從第一個繫結至 Binding 屬性的第一個 SlowestDP 開始。 處理此 Binding 時,它不會成功傳回值,因為它已睡眠 5 秒,因此會處理下一個 Binding 元素。 下一個 Binding 不會成功傳回值,因為它已睡眠 3 秒。 接著,繫結引擎會移至下一個 Binding 元素,該元素會繫結至 FastDP 屬性。 此 Binding 會傳回 "Fast Value" 值。 TextBlock 現在會顯示 "Fast Value" 值。

經過 3 秒之後,SlowerDP 屬性會傳回 "Slower Value" 值。 TextBlock 接著會顯示 "Slower Value" 值。

經過 5 秒之後,SlowestDP 屬性會傳回 "Slower Value" 值。 該繫結具有最高的優先順序,因為它會先列出。 TextBlock 現在會顯示 "Slowest Value" 值。

如需什麼是從繫結成功傳回值的相關資訊,請參閱 PriorityBinding

另請參閱