WPF - access to children from a trigger

I have a ListBox as I show there. The fact is that in determining the subject I have a lot of material with grids and elements. I want to change the visibility of one image in an element only in order to make it visible only when the element itself is selected.

I managed to change, for example, the background and general appearance of the element when it is selected, but I can not access the internal elements :(

 <ListBox stuff stuff stuff> <ListBox.ItemTemplate> <DataTemplate DataType="local:Patient"> <grids , borders, things, stuff <Image Name="image1" source opacity stuff/> </ grids bordes and design in general> </DataTemplate> </ListBox.ItemTemplate> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Style.Triggers> <Trigger Property="IsSelected" Value="True"> <Setter Property="Foreground" Value="White"/> <!--HERE I WANT TO CHANGE VISIBILITY OF THE IMAGE--> </Trigger> </Style.Triggers> </Style> </ListBox.ItemContainerStyle> <ListBox.Template> <!-- some other styles when deselected and other things --> </ListBox.Template> </ListBox> 

I tried using:

 <Setter TargetName="physiciansSettinsImage" Property="Visibility" Value="Visible"/> 

But it cannot be installed in Style Setter. Any clue?

The whole design is quite complex, so I would like to avoid as much conversion as possible.

+4
source share
1 answer

Move the cursor to the DataTemplate .

I believe image1 Visibility is Collapsed

 <DataTemplate.Triggers> <DataTrigger Binding="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType= {x:Type ListBoxItem}},Path=IsSelected}" Value="True"> <Setter TargetName="image1" Property="Visibility" Value="Visible"/> </DataTrigger> </DataTemplate.Triggers> 

Now that the item is selected, your Image Visibility set to Visible .

+5
source

All Articles