Russ
Russ

Reputation: 12530

How can I trigger a control event when a ListViewItem is selected?

I have an image that I want to show only when a ListViewItem is selected. The code I have isn't working, but I think it illustrates what I want to accomplish.

<GridViewColumn>
   <GridViewColumn.CellTemplate>
      <DataTemplate>
         <Image Height="20">
            <Image.Style>
               <Style TargetType="{x:Type Image}">
                  <Setter Property="Visibility" Value="Collapsed" />
                  <Style.Triggers>
                     <DataTrigger Binding="{Binding RelativeSource=
                           {
                              RelativeSource 
                              Mode=FindAncestor, 
                              AncestorType={x:Type ListViewItem}
                           }, Path=IsSelected}" Value="True">
                        <Setter Property="Visibility" Value="Visible" />
                        <Setter Property="Source" Value="/Russound.Windows;component/Resources/2leftarrow-64.png" />
                        <Setter Property="ToolTip" Value="Selected" />
                     </DataTrigger>
                  </Style.Triggers>
               </Style>
            </Image.Style>
         </Image>
      </DataTemplate>
   </GridViewColumn.CellTemplate>
</GridViewColumn>

Upvotes: 0

Views: 1230

Answers (2)

Trainee4Life
Trainee4Life

Reputation: 2273

When dealing with binding issues, I generally search through the Output window to find any binding errors. They all start with System.Windows.DataError. So, are there any errors in the Output window?

Upvotes: 1

japf
japf

Reputation: 6718

Simply change the default Visibility to Hidden instead of Collapsed.

Apparently, if you use Collapsed, the element is removed from the visual tree and the RelativeSource no longer works.

Upvotes: 0

Related Questions