title: Wpf How to change List box ItemspanelTemplate using Trigger
body: <p>I need to set the <code>ItemsPanelTemplate</code> property of a <code>ListBox</code> based on a <code>DependencyProperty</code> on the control. How do I use the <code>DataTemplateSelector</code> to do that?</p>
  
  <p>I have something like:   </p>
  
  <pre><code>&lt;ListBox.ItemsPanel&gt;
     &lt;ItemsPanelTemplate&gt;
         &lt;!-- Here I need to replace with either a StackPanel or a wrap panel--&gt;
     &lt;/ItemsPanelTemplate&gt;
  &lt;/ListBox.ItemsPanel&gt;
  </code></pre>
  
  <p>Here (<a href="https://stackoverflow.com/questions/10652639/itemspaneltemplate-selector-in-wpf">ItemsPanelTemplate Selector in wpf?</a>) is link with <em>Similar Question</em>. Below is my code but its not working:</p>
  
  <pre><code>&lt;Window x:Class="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;XmlDataProvider x:Key="myXmlDataBase" XPath="/myXmlData"&gt;
              &lt;x:XData&gt;
                  &lt;myXmlData xmlns=""&gt;
                      &lt;Item Name = "CoverSheet" SNo="1"  Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000003.tif"/&gt;
                      &lt;Item Name = "HCFA" SNo="2" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/&gt;
                      &lt;Item Name = "HCFA" SNo="3" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/&gt;
                      &lt;Item Name = "HCFA" SNo="4" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/&gt;
                      &lt;Item Name = "HCFA" SNo="5" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/&gt;
                      &lt;Item Name = "HCFA" SNo="6" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000004.tif"/&gt;
                      &lt;Item Name = "HCFA_CC" SNo="7" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\00000005.tif"/&gt;
                      &lt;Item Name = "FrontPage" SNo="8" Type="GrpBX" Image="C:\Work\00288511851128436163\N12201_0003_003\N12201_0003_003I00.tif"/&gt;
                  &lt;/myXmlData&gt;
              &lt;/x:XData&gt;
          &lt;/XmlDataProvider&gt;            
      &lt;/Window.Resources&gt;
  
      &lt;DockPanel&gt;
          &lt;ListBox Name="lv" ItemsSource="{Binding Source={StaticResource myXmlDataBase},XPath=Item}" FontSize="12" Background="LightGreen" ItemsPanel="{Binding RelativeSource={RelativeSource Self}, Path=Background}"&gt;
               &lt;ListBox.Resources&gt;                    
                   &lt;Style x:Key="ListBoxWrapStyle" TargetType="ListBox"&gt;
                       &lt;Setter Property="ItemsPanel"&gt;
                           &lt;Setter.Value&gt;
                               &lt;ItemsPanelTemplate&gt;
                                   &lt;WrapPanel Width="{Binding FrameworkElement.ActualWidth),RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}" ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}" /&gt;
                               &lt;/ItemsPanelTemplate&gt;
                           &lt;/Setter.Value&gt;
                       &lt;/Setter&gt;
                   &lt;/Style&gt;
  
                   &lt;Style x:Key="ListBoxHorizontalStackStyle" TargetType="ListBox"&gt;
                       &lt;Setter Property="ItemsPanel"&gt;
                           &lt;Setter.Value&gt;
                               &lt;ItemsPanelTemplate&gt;
                                  &lt;StackPanel  Width="{Binding (FrameworkElement.ActualWidth),RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" Orientation="Horizontal" /&gt;
                               &lt;/ItemsPanelTemplate&gt;
                           &lt;/Setter.Value&gt;
                       &lt;/Setter&gt;
                   &lt;/Style&gt;
  
                   &lt;Style x:Key="ListBoxVerticalStackStyle" TargetType="ListBox"&gt;
                       &lt;Setter Property="ItemsPanel"&gt;
                           &lt;Setter.Value&gt;
                               &lt;ItemsPanelTemplate&gt;
                                   &lt;StackPanel Width="{Binding (FrameworkElement.ActualWidth),RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" Orientation="Vertical" /&gt;
                               &lt;/ItemsPanelTemplate&gt;
                           &lt;/Setter.Value&gt;
                       &lt;/Setter&gt;
                   &lt;/Style&gt;
               &lt;/ListBox.Resources&gt;
  
               &lt;ListBox.Style&gt;
                   &lt;Style TargetType="ListBox"&gt;
                       &lt;Style.Triggers&gt;
                           &lt;!-- Your Trigger.. --&gt;
                           &lt;Trigger Property="Background" Value="Green"&gt;
                               &lt;Setter Property="ItemsPanel" Value="{StaticResource ListBoxVerticalStackStyle}"/&gt;
                           &lt;/Trigger&gt;
  
                           &lt;Trigger Property="Background" Value="LightBlue"&gt;
                               &lt;Setter Property="ItemsPanel" Value="{StaticResource ListBoxHorizontalStackStyle}"/&gt;
                           &lt;/Trigger&gt;
  
                           &lt;Trigger Property="Background" Value="LightGreen"&gt;
                               &lt;Setter Property="ItemsPanel" Value="{StaticResource ListBoxWrapStyle}"/&gt;
                           &lt;/Trigger&gt;
                        &lt;/Style.Triggers&gt;
                   &lt;/Style&gt;
               &lt;/ListBox.Style&gt;
  
               &lt;ListBox.ItemTemplate&gt;
                   &lt;DataTemplate&gt;
                       &lt;Viewbox Stretch="Fill" HorizontalAlignment="Stretch" &gt;
                           &lt;Border BorderThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DataContext="{Binding}" BorderBrush="IndianRed"   Margin="0" Height="Auto"&gt;
                              &lt;DockPanel&gt;
                                  &lt;Image 
                                      DockPanel.Dock="Top"
                                      Width="150" Margin="5"
                                      HorizontalAlignment="Stretch"
                                      VerticalAlignment="Stretch"
                                      Height="Auto"  x:Name="Myimage" 
                                      RenderOptions.BitmapScalingMode="HighQuality"  Source="{Binding XPath=@Image}"&gt;
                                  &lt;/Image&gt;
  
                                  &lt;Grid&gt;
                                      &lt;TextBlock Text="{Binding XPath=@SNo}" HorizontalAlignment="Center" FontWeight="Normal" FontSize="13" /&gt;
                                  &lt;/Grid&gt;
                              &lt;/DockPanel&gt;
                          &lt;/Border&gt;
                      &lt;/Viewbox&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
      &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>While running this code I'm getting <strong>ListBoxVerticalStackStyle</strong>'only.</p>