2014-04-29 23 views
0

我在問如何將多個數組綁定到列表框中。這是例如如何用一個陣列和在列表框中一個文本塊做數組和列表框

string[] Name={Terry, John, Edvard}; 

for(int i=0;i<Name.Lenght, i++) 
ListBoxName.Items.Add(Name[i]); 

XAML:

<ListBox x:Name="ListBoxName"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <TextBlock Name=TextBlockName Text="{Binding}" /> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
</ListBox> 

確定。這不是問題。問題開始,如果想其他陣列添加到其他文本塊在同一個列表框:

string[] Name={Terry, John, Edvard}; 
string[] id= {122, 234, 665}; 

//?? 

XAML:

<ListBox x:Name="ListBoxName"> 
         <ListBox.ItemTemplate> 
          <DataTemplate> 
           <TextBlock Name="TextBlockName"/> 
           <TextBlock Name="TextBlockid"/> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
    </ListBox> 

我對WindowsPhone的應用8工作。這僅僅是一個例子。

謝謝你的幫助。

+1

究竟是什麼問題?你是否收到編譯錯誤? – Latheesan

+1

爲什麼不加入列表並在列表框中輸出包含所需屬性的新對象。 – Shoe

+0

沒有編譯問題我只想要一些例子如何顯示或加載兩個數組在一個列表框中。 – user3585879

回答

0

在這裏你會需要一個新的類,在所有的數組作爲屬性的元素,如:

代碼:

Class MyObject 
{ 
    public string Name{get;set;} 
    public string ID{get;set;} 
} 

MyObject[] MyObjectList = {new MyObject{Name="Terry",ID="122"}, new MyObject{Name="John",ID="234"},new MyObject{Name="Edvard",ID="665"}}; 

for(int i=0;i<MyObjectList.Lenght, i++) 
ListBoxName.Items.Add(MyObjectList[i]); 

XAML

<ListBox x:Name="ListBoxName"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <TextBlock Name="TextBlockName" Text="{Binding Path=Name}" /> 
          <TextBlock Name="TextBlockid" Text="{Binding Path=ID}"/> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
</ListBox> 

這將解決你的問題,但像@Abbas也建議的那樣,看看Data Binding

+0

這正是我所需要的。我很抱歉,因爲這個問題真的很奇怪。 謝謝大家 – user3585879

0

我想向您推薦一個關於SO的問題:How can I data bind a list of strings to a ListBox in WPF/WP7?。底線是,我建議你使用databinding,而不是遍歷數組並添加項目。使用數據綁定,一個類來保存屬性和MVVM模式,您可以將視圖模型分配給視圖。您將此視圖模型綁定到提取正確信息的視圖。

簡單的例子(從我的回答)有點適應您的情況:

XAML:

<ListBox Margin="20" ItemsSource="{Binding Items}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding Path=Name}" /> 
       <TextBlock Text="{Binding Path=ID}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

代碼:

public class NameIdClass 
{ 
    public string Name { get; set; } 
    public string ID{ get; set; } 
} 

public class ViewModel 
{ 
    public List<NameIdClass> Items 
    { 
     get 
     { 
      return new List<NameIdClass> 
      { 
       new NameIdClass { Name = "Terry", ID = 122 }, 
       new NameIdClass { Name = "John", ID = 234 } 
      }; 
     } 
    } 
} 

//This can be done in the Loaded event of the page: 
DataContext = new MyViewModel(); 

希望這有助於。

0

我覺得你的問題是類似這樣的問題 Load a listbox with two textblock from a database with LINQ windows phone

如果您的問題被合併兩個數組(C#3.0),那麼它的那樣簡單

int[] first = { 1,2,3,4 }; // use var instead array for illustration 
int[] second = { 5,6,7,8 }; 
int[] combined = first.Concat(second).ToArray(); 

如果這沒有解決您的問題,請提供更多信息,確切的錯誤是什麼。