I have a listview that you select a row/item on. This is linked to a datatrigger that displays an image on the row. The image should only be displayed when the row is selected.
This part works fine, however when you move the focus to something else, such as a textbox, or a messagebox is displayed, the listviewitem loses focus ie the highlight on the row is no longer displayed. The problem is that my image still remains. It should be hidden/collapsed when the listview loses focus… It works fine if you select a different item/row on the listview.
Can anyone help on this please?
<Style x:Key='deleteImageStyle' TargetType='{x:Type Image}'> <Setter Property='Source' Value='Resources/iconDelete.png' /> <Setter Property='Margin' Value='0,2,5,0' /> <Setter Property='Height' Value='16' /> <Setter Property='Width' Value='16' /> <Setter Property='HorizontalAlignment' Value='Right' /> <Setter Property='VerticalAlignment' Value='Top' /> <Setter Property='Cursor' Value='Hand' /> <Style.Triggers> <DataTrigger Binding='{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}' Value='True'> <Setter Property='Visibility' Value='Visible'/> </DataTrigger> <DataTrigger Binding='{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}' Value='False'> <Setter Property='Visibility' Value='Hidden'/> </DataTrigger> <Trigger Property='IsEnabled' Value='False'> <Setter Property='Visibility' Value='Hidden' /> </Trigger> </Style.Triggers> </Style>
Regards
TravisPUK
I think you’re confusing IsSelected and IsFocused.
Experiment with binding your triggers to IsFocused instead of IsSelected to get your desired result.
If i understand correctly, you only want the image to be visible if both IsSelected and IsFocused are true, otherwise hidden.
One way to do this is to set default Visibility to Visible, and then add two triggers that set Visibility to Hidden: one trigger for IsSelected = False, and another trigger for IsFocused = False.
Or the opposite, set default Visibility to Hidden, and use a MultiTrigger with IsSelected = True and IsFocused = True to set it Visibility to Visible