How to get selected item of a nested listview?

Deadly 提交于 2019-12-11 23:39:31

问题


i have a nested listview, i can bind the selected item of the basic listview to my viewmodel but not my selected item of the nested listview ( in the basic listview ) I just do: this is my listview:

      <ListView Height="155" ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible"   dd:DragDrop.IsDragSource="True" 
  dd:DragDrop.IsDropTarget="False" Margin="24,506,280,169" Background="#CDC5CBC5"
                 dd:DragDrop.DropHandler="{Binding}" SelectedItem ="{Binding Path=SelectedCluster,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" ItemsSource="{Binding Path=Clusters,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" >
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Titel" DisplayMemberBinding="{Binding Title}"/>
                        <GridViewColumn Header="Questions">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <ListView DataContext="{Binding}" ItemsSource="{Binding ExaminationQuestions}" SelectedItem="{Binding Path=SelectedExaminationQuestionInCluster,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}">
                                        <ListView.View>
                                            <GridView>
                                                <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Question.Description}"/>


                                            </GridView>
                                        </ListView.View>
                                    </ListView>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>

Viewmodel:

public ExaminationQuestion SelectedExaminationQuestionInCluster
        {
            get { return selectedExaminationQuestionInCluster; }
            set { selectedExaminationQuestionInCluster = value;
            OnPropertyChanged("SelectedExaminationQuestionInCluster");
            }
        }

Someone who knows what i am doing wrong? If i set a breakpoint of the setter of selecteditem of the second listview. He just ignores that..

Thanks


回答1:


My guess is the binding is probably incorrect. In your outer ListView, you bind to "Clusters". Your inner ListView is probably trying to bind to "SelectedExaminationQuestionInCluster" on the current Cluster. You can see if this is the case by using snoop. It's a valuable tool when debugging WPF apps. It will highlight broken bindings in red and tell you what's wrong with them.

If you want to bind to "SelectedExaminationQuestionInCluster" on the parent DataContext, you could use this syntax:

SelectedItem="{Binding Path=DataContext.SelectedExaminationQuestionInCluster,
               ElementName=OuterListView}"

You'll have to give the outer ListView a name of course.

EDIT: I just realized this might not make sense. If each Cluster has its own collection of ExaminationQuestions, then each Cluster should also have a SelectedExaminationQuestion. The parent DataContext should not have any concept of a SelectedQuestion unless it is shared amongst all Clusters.



来源:https://stackoverflow.com/questions/5729916/how-to-get-selected-item-of-a-nested-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!