DataTrigger.Value 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定要比較的資料物件屬性值的值。
public:
property System::Object ^ Value { System::Object ^ get(); void set(System::Object ^ value); };
[System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)]
[System.Windows.Markup.DependsOn("Binding")]
public object Value { get; set; }
[<System.Windows.Localizability(System.Windows.LocalizationCategory.None, Readability=System.Windows.Readability.Unreadable)>]
[<System.Windows.Markup.DependsOn("Binding")>]
member this.Value : obj with get, set
Public Property Value As Object
屬性值
預設值為 null。 另見例外條款章節。
- 屬性
例外狀況
表達式不被支援。 不支援綁定。
範例
在以下範例中, ItemsSource 的 ListBox 被束縛於 Places,一個 ObservableCollection<T> 的 Place 物件。
Place 物件具有屬性 Name 和 State。
每個 ListBoxItem 展區 ListBox 都展示一個物件 Place 。 範例中的 將 Style 應用於每個 ListBoxItem。
DataTrigger指定為若資料項目的前景StatePlace為「WA」,則對應ListBoxItem項目的前景為紅色。
<Window.Resources>
<c:Places x:Key="PlacesData"/>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=State}" Value="WA">
<Setter Property="Foreground" Value="Red" />
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Name}" Value="Portland" />
<Condition Binding="{Binding Path=State}" Value="OR" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="Cyan" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
<DataTemplate DataType="{x:Type c:Place}">
<Canvas Width="160" Height="20">
<TextBlock FontSize="12"
Width="130" Canvas.Left="0" Text="{Binding Path=Name}"/>
<TextBlock FontSize="12" Width="30"
Canvas.Left="130" Text="{Binding Path=State}"/>
</Canvas>
</DataTemplate>
</Window.Resources>
<StackPanel>
<TextBlock FontSize="18" Margin="5" FontWeight="Bold"
HorizontalAlignment="Center">Data Trigger Sample</TextBlock>
<ListBox Width="180" HorizontalAlignment="Center" Background="Honeydew"
ItemsSource="{Binding Source={StaticResource PlacesData}}"/>
</StackPanel>
以下範例展示了兩個 DataTrigger定義在 中的 DataTemplates。
DataTemplate該 應用於AuctionItem資料物件(此例中未顯示),其性質SpecialFeatures為 。
第一個 DataTrigger 設定是若資料物件值 SpecialFeatures 為 Color,則該項目會以 DodgerBlue 背景搭配海軍藍標題顯示。 如果資料物件的 SpecialFeatures 值為 Highlight,則第二個 DataTrigger 物件會是啟用的,導致該項目以橘色邊框並帶有星號顯示。
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=SpecialFeatures}">
<DataTrigger.Value>
<src:SpecialFeatures>Color</src:SpecialFeatures>
</DataTrigger.Value>
<DataTrigger.Setters>
<Setter Property="BorderBrush" Value="DodgerBlue" TargetName="border" />
<Setter Property="Foreground" Value="Navy" TargetName="descriptionTitle" />
<Setter Property="Foreground" Value="Navy" TargetName="currentPriceTitle" />
<Setter Property="BorderThickness" Value="3" TargetName="border" />
<Setter Property="Padding" Value="5" TargetName="border" />
</DataTrigger.Setters>
</DataTrigger>
<DataTrigger Binding="{Binding Path=SpecialFeatures}">
<DataTrigger.Value>
<src:SpecialFeatures>Highlight</src:SpecialFeatures>
</DataTrigger.Value>
<Setter Property="BorderBrush" Value="Orange" TargetName="border" />
<Setter Property="Foreground" Value="Navy" TargetName="descriptionTitle" />
<Setter Property="Foreground" Value="Navy" TargetName="currentPriceTitle" />
<Setter Property="Visibility" Value="Visible" TargetName="star" />
<Setter Property="BorderThickness" Value="3" TargetName="border" />
<Setter Property="Padding" Value="5" TargetName="border" />
</DataTrigger>
</DataTemplate.Triggers>
備註
XAML 屬性元素的使用
<object>
<object.Value>
Value
</object.Value>
</object>
此價值與由 Binding 該 DataTrigger財產產生的財產價值比較。 比較是參考資料的平等性檢查。 若兩個值相等,則會套用相應的動作或設定器。
備註
你必須在 a DataTrigger 上同時指定 和 BindingValue 屬性,資料觸發才有意義。 如果一個或兩個屬性都沒設定,就會拋出例外。