title: Using styles within a data template
body: <p>I have a class called item, and it contains just two properties. I will display them on the screen as buttons with a style. This question relates to how I can style the button based on the IsSelected value, when the element I want to affect is in the style not the data template. I have already tried with a Trigger but been unable to get it to work.</p>
  
  <p>The class is below.</p>
  
  <pre><code>public class Item : ObservableObject
  {
      private string _title;
      private bool _isSelected;
  
      public string Title
      {
          get { return _title; }
          set
          {
              _title = value;
              RaisePropertyChanged("Title");
          }
      }
  
      public bool IsSelected
      {
          get { return _isSelected; }
          set
          {
              _isSelected = value;
              RaisePropertyChanged("IsSelected");
          }
      }
  }
  </code></pre>
  
  <p>I use a data template to display these items in a ItemsControls.</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource ResourceKey=ItemTemplate}"&gt;
          &lt;ItemsControl.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;StackPanel Orientation="Horizontal" /&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ItemsControl.ItemsPanel&gt;
      &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>Using the following style and data template.</p>
  
  <pre><code>&lt;Style x:Key="ItemButton" TargetType="Button"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type Button}"&gt;
                  &lt;Border Name="ButtonBorder" BorderThickness="2,2,2,0" BorderBrush="#AAAAAA"  CornerRadius="6,6,0,0"  Margin="2,20,0,0" Background="Black"&gt;
                      &lt;ContentPresenter
                              VerticalAlignment="Center"  
                              HorizontalAlignment="Center"/&gt;
                  &lt;/Border&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  &lt;DataTemplate x:Key="ItemTemplate"&gt;
      &lt;Button Height="60" Style="{StaticResource ItemButton}" Name="Button"&gt;
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;TextBlock Text="{Binding Path=Title}" 
                         HorizontalAlignment="Left" Margin="5,5,5,3" FontSize="25" Foreground="#6B6B6B" FontFamily="Arial" /&gt;
                  &lt;Button Style="{StaticResource NoChromeButton}" Margin="0,0,5,0"&gt;
                  &lt;Button.Content&gt;
                      &lt;Image Height="20" Source="/WpfApplication1;component/Image/dialogCloseButton.png"&gt;&lt;/Image&gt;
                  &lt;/Button.Content&gt;
                  &lt;Button.ToolTip&gt;
                      Close    
                  &lt;/Button.ToolTip&gt;
              &lt;/Button&gt;
          &lt;/StackPanel&gt;
      &lt;/Button&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>I need to change the Background of "ButtonBorder" from Black to White when IsSelected is True, on the object Item. </p>
  
  <p>I have added in a Trigger in the Data Template
          
              
                  
              
          
  This does not work, I guess its because the style overrides the DataTemplate, thus the background stays white. Yet when i try to do a trigger in the style, I can't access the property IsSelected?</p>
  
  <p>DataTemplate Trigger</p>
  
  <pre><code>    &lt;DataTemplate.Triggers&gt;
          &lt;DataTrigger Binding="{Binding IsSelected}" Value="True"&gt;
              &lt;Setter TargetName="Button" Property="Background" Value="White"/&gt;
          &lt;/DataTrigger&gt;
      &lt;/DataTemplate.Triggers&gt;
  </code></pre>
  
  <p>Style trigger</p>
  
  <pre><code>    &lt;Style.Triggers&gt;
          &lt;DataTrigger Binding="{Binding IsSelected}" Value="True"&gt;
              &lt;Setter Property="Background" Value="White"/&gt;
          &lt;/DataTrigger&gt;
      &lt;/Style.Triggers&gt;
  </code></pre>
  
  <p>Am i missing something?</p>