次の方法で共有


DataTrigger.Value プロパティ

定義

データ オブジェクトのプロパティ値と比較する値を取得または設定します。

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 です。 「例外」セクションも参照してください。

属性

例外

式はサポートされていません。 バインドはサポートされていません。

次の例では、ListBoxItemsSourceは、Place オブジェクトのObservableCollection<T>であるPlacesにバインドされています。 Place オブジェクトには、プロパティ NameStateがあります。

ListBoxの各ListBoxItemには、Place オブジェクトが表示されます。 この例の Style は、各 ListBoxItemに適用されます。

DataTriggerは、Place データ項目のStateが "WA" の場合、対応するListBoxItemの前景が Red に設定されるように指定されます。

<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>

次の例は、DataTemplateで定義されている 2 つのDataTriggerを示しています。 DataTemplateは、プロパティがSpecialFeaturesAuctionItemデータ オブジェクト (この例では示されていません) に適用されます。

最初のDataTriggerは、データ オブジェクトに ColorSpecialFeatures値がある場合は、アイテムが海軍のタイトルを持つドッジャーブルーの背景で表示されるように指定されます。 データ オブジェクトの SpecialFeatures 値が Highlight の場合、2 番目の 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>

この値は、DataTriggerBinding プロパティによって生成されるプロパティ値と比較されます。 比較は参照等値チェックです。 2 つの値が等しい場合は、関連付けられているアクションまたはセッターが適用されます。

データ トリガーが意味を持つには、DataTriggerBindingプロパティとValue プロパティの両方を指定する必要があります。 一方または両方のプロパティが設定されていない場合は、例外がスローされます。

適用対象

こちらもご覧ください