views:

432

answers:

3

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>
A: 

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.

Jalfp
Changing to Hidden is a No Go Jalfp
Russ
I built a sample application that works using your code with Hidden instead of Collapsed. Maybe I didn't understand what you want to accomplish...
Jalfp
+1  A: 

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?

Trainee4Life
Well, I will say that I did not know about the DataError in the output window. Thank you, I fixed several other binding problems, HOWEVER, this specific issue is not throwing an error.
Russ
Oh, yea sorry, +1 for teaching me something new.
Russ
Try this, have two DataTriggers, one when IsSelected is true and the other one for false. And also remove that Setter. What I'm suspecting is that dependency property resolution is always picking that setter value for it has a higher precedence than style triggers. I have faced this problem before, but I don't really remember the exact situation.
Trainee4Life
For more information, check MSDN @ http://msdn.microsoft.com/en-us/library/ms743230.aspx
Trainee4Life
A: 

Check below link

http://asimsajjad.blogspot.com/2009/05/wpf-listbox-control.html

Hope that will help you.

Asim Sajjad
Not 100% perfect, but its pointing me in the right direction. Thanks.
Russ