Tutorial :WPF ListViewItem lost focus event - How to get at the event?



Question:

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


Solution:1

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


Solution:2

@Bubblewrap,

Thanks for the information, this pretty well got around the issue. As per below I had to add in both scenarios as the default doesn't seem to take effect... however I haven't tried the MultiTrigger method yet, will do later.

This is what I ended up with in the end.

<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=IsFocused}" 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>              <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsFocused}" Value="False">                  <Setter Property="Visibility" Value="Hidden"/>              </DataTrigger>              <Trigger Property="IsEnabled" Value="False">                  <Setter Property="Visibility" Value="Hidden" />              </Trigger>          </Style.Triggers>  

Thanks for your help, this will get me past my issue for now. I think that my IsEnabled trigger is probably redundant now though.

Thanks

TravisPUK


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »