Вопрос

I am asking how to bind more than one array into listbox. This is example how a did with one array and one textblock in listbox

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>

Ok. This is not a problem. Problems starts if a want to add other array to other textblock in same 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>

I am working on WindowsPhone 8 application. This is just example.

Thank you for any help.

Это было полезно?

Решение

You will need a new class here with elements in all your arrays as properties, e.g.:

Code:

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>

This will solve your problem, but do have a look of Data Binding as @Abbas also suggested.

Другие советы

I would like to refer you to an answer of mine on a question here on SO: How can I data bind a list of strings to a ListBox in WPF/WP7?.

Bottom line is, I suggest you use databinding instead of iterating over an array and adding items. Using databinding, a class to hold properties and the MVVM pattern you can assign a viewmodel to the view. You bind this viewmodel to the view in which you extract the right information.

Short example (from my answer) a bit adapted to your situation:

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> 

Code:

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();

Hope this helps.

I think your problem is similar to this question Load a listbox with two textblock from a database with LINQ windows phone

If your issue is merging the two arrays (c# 3.0) then its as simple as

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

If this doesn't resolve your issue then please provide more information on what is the exact error.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top