Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The parent element whose style you want to inspect is not the direct parent of the TextBlock; it could be arbitrarily higher in the visual tree. So your second condition needs to look for an ancestor of a particular type like this:</p> <pre><code>&lt;Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=Style}" Value="{StaticResource Suggestion}" /&gt; </code></pre> <p>Here is a complete working example, tested with .NET4:</p> <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;Style x:Key="Suggestion" TargetType="{x:Type ListBoxItem}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ListBoxItem"&gt; &lt;Grid Name="Container" Margin="0,0,0,0"&gt; &lt;Rectangle Margin="0,2,0,2" Stroke="Blue" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;&lt;/Rectangle&gt; &lt;Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3" SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;&lt;/Rectangle&gt; &lt;ContentPresenter Margin="0"/&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style x:Key="PinnedSuggestion" TargetType="{x:Type ListBoxItem}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ListBoxItem"&gt; &lt;Grid Name="Container" Margin="0,0,0,0"&gt; &lt;Rectangle Margin="0,2,0,2" Stroke="Green" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;&lt;/Rectangle&gt; &lt;Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3" SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;&lt;/Rectangle&gt; &lt;ContentPresenter Margin="0"/&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style x:Key="Neutral" TargetType="{x:Type ListBoxItem}"&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="ListBoxItem"&gt; &lt;Grid Name="Container" Margin="0,0,0,0"&gt; &lt;Rectangle Margin="0,2,0,2" Stroke="Black" Name="Background" SnapsToDevicePixels="True" RadiusX="7" RadiusY="7" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;&lt;/Rectangle&gt; &lt;Rectangle Margin="2,4,2,4" Name="BackgroundTwo" StrokeThickness="3" SnapsToDevicePixels="True" RadiusX="3" RadiusY="3" Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt;&lt;/Rectangle&gt; &lt;ContentPresenter Margin="0"/&gt; &lt;/Grid&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;Style TargetType="{x:Type TextBlock}"&gt; &lt;Style.Triggers&gt; &lt;MultiDataTrigger&gt; &lt;MultiDataTrigger.Conditions&gt; &lt;Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" /&gt; &lt;Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=Style}" Value="{StaticResource Suggestion}" /&gt; &lt;/MultiDataTrigger.Conditions&gt; &lt;Setter Property="Foreground" Value="Red"&gt;&lt;/Setter&gt; &lt;/MultiDataTrigger&gt; &lt;MultiDataTrigger&gt; &lt;MultiDataTrigger.Conditions&gt; &lt;Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FontSize}" Value="11" /&gt; &lt;Condition Binding="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=Style}" Value="{StaticResource PinnedSuggestion}" /&gt; &lt;/MultiDataTrigger.Conditions&gt; &lt;Setter Property="Foreground" Value="Yellow"&gt;&lt;/Setter&gt; &lt;/MultiDataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;ListBox&gt; &lt;ListBoxItem Style="{StaticResource Neutral}"&gt; &lt;TextBlock FontSize="10" Text="Style=Neutral, FontSize=10"/&gt; &lt;/ListBoxItem&gt; &lt;ListBoxItem Style="{StaticResource Neutral}"&gt; &lt;TextBlock FontSize="11" Text="Style=Neutral, FontSize=11"/&gt; &lt;/ListBoxItem&gt; &lt;ListBoxItem Style="{StaticResource Suggestion}"&gt; &lt;TextBlock FontSize="10" Text="Style=Suggestion, FontSize=10"/&gt; &lt;/ListBoxItem&gt; &lt;ListBoxItem Style="{StaticResource Suggestion}"&gt; &lt;TextBlock FontSize="11" Text="Style=Suggestion, FontSize=11"/&gt; &lt;/ListBoxItem&gt; &lt;ListBoxItem Style="{StaticResource PinnedSuggestion}"&gt; &lt;TextBlock FontSize="10" Text="Style=PinnedSuggestion, FontSize=10"/&gt; &lt;/ListBoxItem&gt; &lt;ListBoxItem Style="{StaticResource PinnedSuggestion}"&gt; &lt;TextBlock FontSize="11" Text="Style=PinnedSuggestion, FontSize=11"/&gt; &lt;/ListBoxItem&gt; &lt;/ListBox&gt; &lt;/Grid&gt; </code></pre> <p></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload