title: WPF ControlTemplate Triggers
body: <p>I have a project which uses WPF, C# and all that good stuff (Visual Studio 2010 also).</p>
  
  <p>I have a WPF ListBox and, within that ListBox, there is a Control Template for the ListBoxItem. Within that is a section for triggers.</p>
  
  <p>In this particular case, the trigger property is IsSelected, referring to the selected item for the ListBox.</p>
  
  <p>What I wish to do is clean things up by taking this out of the ListBox control and putting it into a ControlTemplate in a resource list. </p>
  
  <p>When I do that, it rightly tells me that there is no 'IsSelected' within the framework element. </p>
  
  <p>Can someone give some suggestions as to how to complete this?</p>
  
  <p>Thanks.</p>
  
  <p>Oh, the XAML code is here:</p>
  
  <pre><code>&lt;ControlTemplate.Triggers&gt;
                  &lt;Trigger Property="IsSelected" Value="true"&gt;
                      &lt;Setter TargetName="_Border" Property="Effect"&gt;
                          &lt;Setter.Value&gt;
                              &lt;DropShadowEffect ShadowDepth="0" Color="Black" Opacity="1" BlurRadius="20" /&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
                  &lt;/Trigger&gt;
              &lt;/ControlTemplate.Triggers&gt;
  </code></pre>
  
  <p>Edit: I did try this:</p>
  
  <pre><code>&lt;Trigger Property="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}" Value="true"&gt;
  </code></pre>
  
  <p>However, I get an error saying 'A 'Binding' cannot be set on the 'Property' property of type 'Trigger''.</p>
  
  <p><strong>Extra edit:</strong> Alright, here is the XAML for the ListBox:</p>
  
  <pre><code>&lt;ListBox ItemsSource="{Binding ChatNodeListViewModel.ChatNodeVMs, Source={StaticResource Locator}}" Background="Transparent" Name="LbNodes" SelectedItem="{Binding ChatNodeListViewModel.SelectedNode, Source={StaticResource Locator}}" &gt;
              &lt;ListBox.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;Canvas HorizontalAlignment="Left" VerticalAlignment="Top" Width="2000" Height="1600"/&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ListBox.ItemsPanel&gt;
              &lt;ListBox.ItemContainerStyle&gt;
                  &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
                      &lt;Setter Property="Template"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ControlTemplate TargetType="ListBoxItem"&gt;
  
                                  &lt;Border x:Name="_Border" Padding="1" SnapsToDevicePixels="true" BorderThickness="3" Margin="2" CornerRadius="5,5,5,5" BorderBrush="{Binding IsHeadNode, Converter={StaticResource ResourceKey=HeadNodeToLinearGradientBrushConverter}}" &gt;
  
                                      &lt;ContentPresenter /&gt;
                                  &lt;/Border&gt;
  
                                  &lt;ControlTemplate.Triggers&gt;
                                      &lt;Trigger Property="IsSelected" Value="true"&gt;
                                          &lt;Setter TargetName="_Border" Property="Effect"&gt;
                                              &lt;Setter.Value&gt;
                                                  &lt;DropShadowEffect ShadowDepth="0" Color="Black" Opacity="1" BlurRadius="20" /&gt;
                                              &lt;/Setter.Value&gt;
                                          &lt;/Setter&gt;
                                      &lt;/Trigger&gt;
                                  &lt;/ControlTemplate.Triggers--&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
  
                      &lt;Setter Property="Canvas.Left" Value="{Binding XCoord}"/&gt;
                      &lt;Setter Property="Canvas.Top" Value="{Binding YCoord}"/&gt;
                      &lt;EventSetter Event="PreviewMouseLeftButtonDown" Handler="lb_PreviewMouseLeftButtonDown" /&gt;
  
                  &lt;/Style&gt;
              &lt;/ListBox.ItemContainerStyle&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Grid&gt;
                          &lt;Thumb Name="myThumb" Template="{StaticResource NodeVisualTemplate}"&gt;
  
                              &lt;i:Interaction.Triggers&gt;
                                  &lt;i:EventTrigger EventName="DragDelta"&gt;
                                      &lt;cmd:EventToCommand Command="{Binding ChatNodeListViewModel.DragDeltaCommand, Source={StaticResource Locator}}" PassEventArgsToCommand="True"/&gt;
                                  &lt;/i:EventTrigger&gt;
  
                              &lt;/i:Interaction.Triggers&gt;
                          &lt;/Thumb&gt;
                      &lt;/Grid&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
  </code></pre>
  
  <p>So if you look at the control template trigger here, it's applied to the border (called '_Border'). Now the problem is that this leaves me with said border separate from the control template for the ListBoxItem (called NodeVisualTemplate). I'd like to put that border into the NodeVisualTemplate, but I'm not sure how I'd go about keeping that link to the IsSelected property. That's kinda the root of the problem.</p>