如何获取嵌套列表视图的选定项目? [英] How to get selected item of a nested listview?

查看:68
本文介绍了如何获取嵌套列表视图的选定项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套的列表视图,我可以将基本列表视图的选定项绑定到我的视图模型,但是不能将我的嵌套列表视图的选定项绑定到我的视图模型(在基本列表视图中),我只是这样做: 这是我的列表视图:

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:

Viewmodel:

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

有人知道我在做什么错吗?如果我设置第二个listview的selecteditem的设置器的断点.他只是无视..

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..

谢谢

推荐答案

我的猜测是绑定可能不正确.在外部ListView中,您绑定到集群".您内部的ListView可能试图绑定到当前集群上的".您可以使用 snoop 查看是否是这种情况.在调试WPF应用程序时,它是一个有价值的工具.它将以红色突出显示断开的绑定,并告诉您它们有什么问题.

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.

如果要绑定到父级DataContext上的"SelectedExaminationQuestionInCluster",则可以使用以下语法:

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

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

您必须给外部ListView指定一个名称.

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

我只是意识到这可能没有道理.如果每个集群都有自己的ExaminationQuestions集合,则每个集群也应该具有SelectedExaminationQuestion.父DataContext不应具有SelectedQuestion的任何概念,除非在所有Cluster之间共享.

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.

这篇关于如何获取嵌套列表视图的选定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆