簡體   English   中英

如何綁定到內部集合

[英]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 我想將BadGood列表中PersonNameImagePath屬性綁定到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; }
    }
}

現在注意,除非您在ViewModels中實現INotifyPropertyChanged並使用ObservableCollection而不是List,否則綁定不會自行更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM