首页 文章

无法绑定嵌套Listview中的选定项目

提问于
浏览
1

我目前使用嵌套在listview中的listview作为以图形方式显示Knockout样式锦标赛的方式,由SectionTreeOne在ViewModel中备份,其中包含对象列表“TournamentNode” . 但是,当我点击它时,我无法将我选择的“锦标赛节点”绑定 .

<Grid Grid.Row="2">
          <ListView ItemsSource="{Binding SectionTreeOne}">
                                <ListView.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <VirtualizingStackPanel Orientation="Horizontal" />
                                    </ItemsPanelTemplate>
                                </ListView.ItemsPanel>
                                <ListView.ItemTemplate >
                                    <DataTemplate>
                                        <ListView ItemsSource="{Binding}" SelectionMode="Single" 
                                                  SelectedItem="{Binding SelectedTournamentNode}">
                                            <ListView.ItemTemplate>
                                                <DataTemplate>
                                                    <TextBlock Text="{Binding Name}" />
                                                </DataTemplate>
                                            </ListView.ItemTemplate>
                                        </ListView>
                                    </DataTemplate>
                                </ListView.ItemTemplate>
                            </ListView>
                        </Grid>

C#绑定:

采集

public List<List<TournamentNodeModel>> SectionTreeOne
            {
                get { return _sectionTreeOne; }
                set
                {
                    _sectionTreeOne = value;
                    base.OnPropertyChanged("SectionTreeOne");
                }
            }

选定项目:

public TournamentNodeModel SelectedTournamentNode
        {
            get { return _selectedTournamentNode; }
            set
            {
                if (value == _selectedTournamentNode)
                    return;

                _selectedTournamentNode = value;
                base.OnPropertyChanged("SelectedTournamentNode");
            }
        }

1 回答

  • 3

    尝试使用以下绑定:

    SelectedItem="{Binding SelectedTournamentNode, Mode=TwoWay}"
    

    请记住,WinRT始终使用 OneWay 绑定模式作为默认值,这与WPF不同,后者根据属性性质或可访问性自动选择绑定模式 .

    我使用WinRT避免这种错误的一个好原则是始终明确指定绑定模式 .


    所以我终于弄明白你绑定中的错误是什么 . 首先,如上所述, SelectedItem 绑定模式必须明确地设置为 TwoWay .

    其次,嵌套列表中 SectionTreeOne 名单绑定到一个内部列表,因此,如果您要绑定 SelectedItem 一个属性上的视图模型,你要这个属性重新绑定使用命名元素的父目录的 DataContext . 您实际上尝试绑定到内部列表上的不存在的属性,而不是绑定到属性所在的视图模型 .

    <ListView x:Name="listView" ItemsSource="{Binding SectionTreeOne}">
        ...
        <ListView.ItemTemplate >
            <DataTemplate>
                <ListView ItemsSource="{Binding}" SelectionMode="Single" 
                          SelectedItem="{Binding Path=DataContext.SelectedTournamentNode, ElementName=listView, Mode=TwoWay}">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    

    请阅读Visual Studio调试器输出,它有关于绑定链中可能发生的绑定错误的非常有用的信息,特别是如果您绑定嵌套在另一个列表中的列表,它将为您节省大量的麻烦!

相关问题