如何绑定到内部集合

How to bind to inner collection

我有很多这样的数据(这是 150 中的 1)

Name = "Andrew",
ImagePath = "Image/Andrew.png",
Bad = new List<Person>
{
    new Person(){Name = "Andrew", ImagePath = "Image/Andrew.png"},
    new Person(){Name = "Andrew", ImagePath = "Image/Aatrox.png"}
},
Good = new List<Person> 
{
    new Person(){Name = "Andrew", ImagePath = "Image/Andrew.png"},
    new Person(){Name = "Andrew", ImagePath = "Image/Andrew.png"}
}

我正在使用 ListView 在 MainPage 上绑定 NameImagePath。我想将列表 BadGoodPersonNameImagePath 属性绑定到 SecondPage,但我不知道如何.

更新:

   public static Person Andrew = new Person() {Name = "Andrew", ImagePath = "Image/Andrew.png"};
        public List<Models> ItemList { get; set; }

        public static List<Models> GetItems()
        {
            return new List<Models>()
            {
                new Models()
                {

                    Name = "Andrew",
                    ImagePath = "Image/Andrew.png",
                    Bad = new List<Person>
                    {Andrew, Andrew},
                    Good = new List<Person> 
                    {Andrew,Andrew}
                }
           }
       }
     }
}

我假设你在绑定到内部集合时遇到了一些问题,我恰好有一些关于绑定到内部集合的示例代码

<Window x:Class="WpfTests.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:designDataContexts="clr-namespace:WpfTests"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <designDataContexts:ViewModel x:Key="vm"/> 
    </Window.Resources>
    <Grid d:DataContext="{StaticResource vm}">
        <ListView ItemsSource="{Binding ItemList}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <WrapPanel>
                        <TextBlock Text="{Binding Name}">
                        </TextBlock>

                        <ListView Name="goodList" ItemsSource="{Binding Good}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Name}"></TextBlock>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </WrapPanel>                    
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

在ViewModel.cs中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfTests
{
    class ViewModel
    {
        public List<Models> ItemList
        {
            get
            {
                return new List<Models>()
                {
                    new Models(),
                    new Models()
                };
            }
        }
    }

    class Models
    {
        public string Name { get { return "test"; } }
        public string ImagePath { get { return "image"; } }

        public List<Person> Good
        {
            get
            {
                return new List<Person>()
                {
                    new Person() {Name = "Name"},
                    new Person() {Name = "Name"},
                    new Person() {Name = "Name"}
                };
            }
        }

        public List<Person> Bad
        {
            get
            {
                return new List<Person>()
            {
                new Person() {Name = "Name"}
            };
            }
        }
    }

    class Person
    {
        public string Name { get; set; }
        public string ImagePath { get; set; }
    }
}

现在请注意,除非您在 ViewModel 中实现 INotifyPropertyChanged 并使用 ObservableCollection 而不是 List

,否则绑定不会自行更新