Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>DataContext</code> of each of the items in your ListBox will be automatically bound to your <code>User</code> instances, so it is not necessary to set the source in your binding. You can bind directly from the style of your <code>ListBoxItem</code>s to properties on your <code>User</code> instances.</p> <p>You can achieve it like this:</p> <pre><code> &lt;Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="ASD_Answer011.MainWindow" x:Name="Window" Title="MainWindow" Width="640" Height="480"&gt; &lt;Window.Resources&gt; &lt;DataTemplate x:Key="ItemTemplate"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="{Binding Property1}"/&gt; &lt;CheckBox IsChecked="{Binding Property2}"/&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}"&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding Path=IsDirty}" Value="True"&gt; &lt;Setter Property="Background" Value="Red" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/&gt; &lt;Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/&gt; &lt;Setter Property="Padding" Value="2,0,0,0"/&gt; &lt;Setter Property="Template"&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt; &lt;Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"&gt; &lt;ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/&gt; &lt;/Border&gt; &lt;ControlTemplate.Triggers&gt; &lt;Trigger Property="IsSelected" Value="true"&gt; &lt;Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/&gt; &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/&gt; &lt;/Trigger&gt; &lt;MultiTrigger&gt; &lt;MultiTrigger.Conditions&gt; &lt;Condition Property="IsSelected" Value="true"/&gt; &lt;Condition Property="Selector.IsSelectionActive" Value="false"/&gt; &lt;/MultiTrigger.Conditions&gt; &lt;Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/&gt; &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/&gt; &lt;/MultiTrigger&gt; &lt;Trigger Property="IsEnabled" Value="false"&gt; &lt;Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/&gt; &lt;/Trigger&gt; &lt;/ControlTemplate.Triggers&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/Window.Resources&gt; &lt;Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}}"&gt; &lt;ListBox ItemTemplate="{DynamicResource ItemTemplate}" ItemsSource="{Binding Collection}" ItemContainerStyle="{DynamicResource ListBoxItemStyle1}"/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>This is how it looks when the application is running:</p> <p><img src="https://i.stack.imgur.com/jlkgt.png" alt="DataTrigger Binding Example"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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