title: WPF - change combobox style depending on itemtemplate
body: <p>I need to create a combo which will show a simple text(enum) value when its drop down is closed. but when user clicks drop down, it should show a complex data template with more information in combo box item.</p>
  
  <p>I have created two data templates to show different information when drop down is closed or opened.</p>
  
  <pre><code>&lt;DataTemplate x:Key="DropDownOpenedTemplate"&gt;
          &lt;ContentControl Content="{Binding}"&gt;
              &lt;ContentControl.Style&gt;
                  &lt;Style TargetType="{x:Type ContentControl}"&gt;
                      &lt;!-- Complex template --&gt;
                      &lt;Setter Property="ContentTemplate"&gt;
                          &lt;Setter.Value&gt;
                              &lt;DataTemplate&gt;
                                  &lt;DockPanel &gt;
                                      &lt;Image Width="100" DockPanel.Dock="Left" Stretch="Uniform" Source="{Binding ImageUri}" 
                                              VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0" /&gt;
                                      &lt;StackPanel DockPanel.Dock="Right" Orientation="Vertical"&gt;
                                          &lt;TextBlock Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Left" FontStyle="Italic" FontWeight="Bold" Margin="10,0,0,5"/&gt;
                                          &lt;TextBlock Text="{Binding Description}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="25,0,0,0"/&gt;
                                      &lt;/StackPanel&gt;
                                  &lt;/DockPanel&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
              &lt;/ContentControl.Style&gt;
          &lt;/ContentControl&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate x:Key="DropDownClosedTemplate" &gt;
          &lt;ContentControl Content="{Binding}"&gt;
              &lt;ContentControl.Style&gt;
                  &lt;Style TargetType="{x:Type ContentControl}"&gt;
                      &lt;!-- default template --&gt;
                      &lt;Setter Property="ContentTemplate"&gt;
                          &lt;Setter.Value&gt;
                              &lt;DataTemplate&gt;
                                  &lt;TextBlock Text="{Binding Name}" HorizontalAlignment="Stretch"  /&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Style&gt;
              &lt;/ContentControl.Style&gt;
          &lt;/ContentControl&gt;
      &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>Then I created a template selector which will override SelectTemplate method</p>
  
  <pre><code>public override DataTemplate SelectTemplate(object item, DependencyObject container)
      {
          ContentPresenter presenter = (ContentPresenter)container;
  
          FrameworkElement elemnt = container as FrameworkElement;
  
          while (container != null)
          {
              container = VisualTreeHelper.GetParent(container);
              if (container is ComboBoxItem)
                  return (DataTemplate)presenter.FindResource("DropDownOpenedTemplate"); ;
          }
          return (DataTemplate)presenter.FindResource("DropDownClosedTemplate");
  
      }
  </code></pre>
  
  <p>This functionality is working correctly. but now I need to format style depending on template used. To simplify, I need background color of combo box to be blue when dropdownclosedtemplate is selected, yellow when dropdownopenedtemplate is selected and selected item should have background as red.</p>
  
  <pre><code>&lt;ComboBox x:Name="spComboBox" Grid.Row="1"
                    Grid.Column="1"
                    Width="200"
                    Margin="30"
                Background="Blue"
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    ItemsSource="{Binding ModeList}" 
                    SelectedValue="{Binding SelectedMode, Mode=TwoWay}" 
                    ScrollViewer.CanContentScroll="False" 
                    ItemTemplateSelector="{StaticResource ComboBoxItemTemplateSelector}"&gt;
          &lt;ComboBox.ItemContainerStyle&gt;
              &lt;Style TargetType="ComboBoxItem"&gt;
                  &lt;Setter Property="Background" Value="Yellow"/&gt;
                  &lt;Style.Triggers&gt;
                      &lt;DataTrigger Binding="{Binding Path=IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Value="False"&gt;
                          &lt;Setter Property="Background" Value="Blue" /&gt;
                      &lt;/DataTrigger&gt;
                  &lt;/Style.Triggers&gt;
              &lt;/Style&gt;
          &lt;/ComboBox.ItemContainerStyle&gt;
  
      &lt;/ComboBox&gt;
  </code></pre>
  
  <p>I can see only yellow color but not able to change style for dropdownclosedtemplate and selecteditem style when dropdownopenedtemplate is selected.</p>
  
  <p>Any help will be appreciated.</p>