简体   繁体   English

如何绑定到内部集合

[英]How to bind to inner collection

I have lots of data like this (this is 1 from 150) 我有很多这样的数据(这是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"}
}

I'm binding Name and ImagePath on MainPage using a ListView . 我使用ListView在MainPage上绑定NameImagePath I want to bind the Name and ImagePath properties of the Person 's in the lists Bad and Good to SecondPage, but I don't know how. 我想将BadGood列表中PersonNameImagePath属性绑定到SecondPage,但是我不知道如何。

Update: 更新:

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

I'm assuming you had some problem with binding to the inner collection and I happen to have some sample code on binding to inner collections 我假设您在绑定到内部集合时遇到一些问题,而我碰巧在绑定到内部集合时有一些示例代码

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

In ViewModel.cs: 在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; }
    }
}

Now notice the bindings won't update themselves unless you implement INotifyPropertyChanged in the ViewModels and use ObservableCollection instead of List 现在注意,除非您在ViewModels中实现INotifyPropertyChanged并使用ObservableCollection而不是List,否则绑定不会自行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM