Answer the question
In order to leave comments, you need to log in
How to use a trigger in WPF to change the picture in a child element of a ListBox?
There is a ListBox, inside which a list of audio is displayed. Each element has a picture. How to make it so that when an element is selected, the image changes to another one? Tried to respond to the trigger on the IsSelected property, the error "Property is not available". And how to write the trigger itself so that it changes the Source property of the image?
<ListBox x:Name="ListAudio"
Margin="10,122,81,10"
MouseDoubleClick="ListAudio_MouseDoubleClick">
<ListBox.ItemTemplate>
<DataTemplate>
<DataTemplate.Triggers>
<Trigger Property="IsSelected" Value="true"></Trigger>
</DataTemplate.Triggers>
<StackPanel Orientation="Horizontal">
<Image Width="22"
Height="22"
Margin="5"
Source="/Images/play.png" />
<StackPanel>
<TextBlock FontWeight="Bold" Text="{Binding Path=title}"></TextBlock>
<TextBlock Text="{Binding Path=duration}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Answer the question
In order to leave comments, you need to log in
Try like this
<Image Width="22"
Height="22"
Margin="5"
>
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem}}" Value="true">
<Setter Property="Image.Source" Value="../Images/play2.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ListBoxItem}}" Value="false">
<Setter Property="Image.Source" Value="../Images/play.png"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question