How to reach SelectedItem in nested ListView

后端 未结 1 367

I\'m wrestling with these last few days. I found lots of links, but none of them really helped me. I\'m quite a beginner in WPF. All I need is to reach SelectedItem property in

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-29 05:17

    In

    SelectedItem="{Binding Path=NestedSelectedItem,
                   RelativeSource={RelativeSource AncestorType=vm:MainVM}, ...}
    

    the type vm:MainVM is not an ancestor type of the inner ListView, because it not part of a visual or logical tree.

    The AncestorType must be a UI element, e.g. the outer ListView. You would access the property by a nested property Path via its DataContext:

    SelectedItem="{Binding Path=DataContext.NestedSelectedItem,
                   RelativeSource={RelativeSource AncestorType=ListView}, ...}
    

    As a note, since you are not setting the View property of the ListViews, you could as well use the simpler base class ListBox instead of ListView:

    
        
            
                
                    
                    
                    
                
            
        
    
    

    0 讨论(0)
提交回复
热议问题