2015-04-23 81 views
0

我有很多這樣的數據(這是1 150)如何綁定列表的列表

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

我在使用的MainPage一個ListView結合NameImagePath。我想將列表BadGood中的PersonNameImagePath的屬性綁定到第二頁,但我不知道如何。

更新:

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

究竟是什麼問題,您是否嘗試過某些內容並得到錯誤?或者你根本不知道用來綁定內部集合的綁定表達式? –

+0

向我們展示如何在MainPage上應用綁定,以及您在SecondPage上嘗試過的內容。順便說一下:你的代碼片段是viewmodel的一部分嗎? – venerik

回答

0

我假設你有一些問題,結合內收集和我正好有結合內收藏

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

在視圖模型的一些示例代碼。 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; } 
    } 
} 

現在注意綁定不會自動更新,除非你在im在ViewModels中使用ObservablePropertyChanged並使用ObservableCollection而不是List

+0

TY這麼多,正是我所需要的:3 –

+0

當一個答案解決了你的問題,不要忘記接受它作爲一個答案(在問題附近的正確標記) –

+0

我有一個問題...在你的答案,ü給一個與另一個ListView datatemplate的例子,問題是,我想要顯示ListView中位於MainPage,如果即時通訊使用DataTemplate列表視圖+文本+圖像ListItem中的SelectedItem的好和壞列表視圖,我有列表視圖從所有列表好和壞 –