如何绑定到内部集合

时间:2015-04-23 20:45:47

标签: c# xaml mvvm windows-phone-8.1

我有很多这样的数据(这是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"}
}

我使用Name在MainPage上绑定ImagePathListView。我想将列表NameImagePathPerson的{​​{1}}和Bad属性绑定到SecondPage,但我不想&# 39;不知道如何。

更新:

Good

1 个答案:

答案 0 :(得分:1)

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

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

现在注意绑定不会自我更新,除非你在ViewModels中实现INotifyPropertyChanged并使用ObservableCollection而不是List