none
Select ListView row when item in embedded ListView is selected

    Pregunta

  • Hello, I have a ListView control that has four columns. One of the columns is another ListView that contains a collection of items. Everything works fine, but I would like to be able to have the parent ListView row selected when an item in the inner ListView is selected. I'm fairly new to WPF so I didn't know if I could access the parent ListView from the child ListView... Any suggestions?
    Goonies 'R Forever
    martes, 22 de marzo de 2011 17:34

Respuestas

  • Add an EventHandler to the inner child ListView SelectionChanged event and set the parent's SelectedItem to the child's DataContext.
    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsbyspot.wordpress.com
    • Marcado como respuesta RiotAct miércoles, 23 de marzo de 2011 9:56
    martes, 22 de marzo de 2011 18:19

Todas las respuestas

  • Add an EventHandler to the inner child ListView SelectionChanged event and set the parent's SelectedItem to the child's DataContext.
    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsbyspot.wordpress.com
    • Marcado como respuesta RiotAct miércoles, 23 de marzo de 2011 9:56
    martes, 22 de marzo de 2011 18:19
  • Hi RiotAct,

    The parent listview item is not selected because the relevent events are handled by the inner listview. To resolve this problem, you can add the following style to the parent ListView to select an item when the keyboard focus moves in.

          <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
              <Style.Triggers>
                <Trigger Property="IsKeyboardFocusWithin" Value="True">
                  <Setter Property="IsSelected" Value="True"/>
                </Trigger>
              </Style.Triggers>
            </Style>
          </ListView.ItemContainerStyle>
    

    Hope this helps.

    If you still have any questions or concerns about this issue, please feel free to let me know.


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    miércoles, 23 de marzo de 2011 8:59
    Moderador
  • Both of those worked, but I went with Bigsby's answer first. Thanks to both of you!
    Goonies 'R Forever
    miércoles, 23 de marzo de 2011 9:56
  • I have used this trigger but have encountered a problem.  When i select a row the SelectedItem is not being properly set and is in fact set to null.  Also ListCollectionView.CurrentPosition of the CollectionViewSource which the ListView uses as a ItemsSource is set to -1.  Any idea why this is the case>
    jueves, 07 de abril de 2011 1:05