Manoj
Manoj

Reputation: 1

Binding list Collection from a list collection in Window Phone 7

How to Binding list Collection from a list collection in Window Phone 7 while i am able to bind from a single list collection

Upvotes: 0

Views: 391

Answers (4)

AVIK DUTTA
AVIK DUTTA

Reputation: 746

Let say we have a ListBox lstbx and a collection lets say

List <String> listdata = new List<String>();

we can add items to the collection by Add() Ex-

listdata.Add("Nazi 1");
    or 
    forloop(expression)
    {
     listdata.Add("vale")
     }

then we can assign assign the collection directly to the listbox' item Source ex.

lstbx.ItemSource=listdata;

//make sure if u are storing more than one variable in a single item of the collection ,you should create custom data template for the ListBox Item Template. !

Upvotes: 0

Rashad Valliyengal
Rashad Valliyengal

Reputation: 3162

You can use the code below,

<ListBox Name="RouteListBox" ItemContainerStyle="{StaticResource RouteListBoxItemStyle}" SelectedItem="{Binding Model.SelectedRoute,Mode=TwoWay}" ItemsSource="{Binding RouteListCollection}">
<i:Interaction.Triggers>
  <i:EventTrigger EventName="Tap">
     <command:EventToCommand Command="{Binding RouteItemSelectedCommand}"/>
  </i:EventTrigger>
</i:Interaction.Triggers>

<ListBox.ItemTemplate>
   <DataTemplate>
       <Grid >
         <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
         </Grid.ColumnDefinitions>

         <TextBlock Text="{Binding RouteName}" Style="{StaticResource RoutesStyle}" Grid.Column="1" />
         <Border  Style="{StaticResource RouteCountBorder}" Visibility="Collapsed" Grid.Column="2">
             <TextBlock Style="{StaticResource RoutesCount}"  Visibility="Collapsed" Text="{Binding ShopCount,Mode=TwoWay}"></TextBlock>
        </Border>

       </Grid>
     </DataTemplate>
  </ListBox.ItemTemplate></ListBox>

Upvotes: 1

Praveen
Praveen

Reputation: 267

First of all have item template in your Xaml. Add Binding to it. Define that binding property in your code. Assign values to the defined property.

I am having a item template in my Xaml like this :

        <Grid.RowDefinitions>
            <RowDefinition Height="367*" />
        </Grid.RowDefinitions>
        <ListBox HorizontalAlignment="Stretch"  Name="lstbNewOrders" Grid.Row="1" HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid x:Name="itemTemplate" Background="Transparent" HorizontalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="250"/>
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="40"/>
                            <RowDefinition Height="20"/>
                        </Grid.RowDefinitions>
                        <TextBlock FontSize="30" Name="txtEbeln" Text="{Binding ebeln}" Grid.Row="0" Grid.Column="0" FontWeight="Bold"  />

                        <TextBlock FontSize="25"   Name="txtCName" Text="{Binding cname}"  Grid.Row="1" Grid.Column="0" />
                        <TextBlock FontSize="25"  Name="txtDate" Text="{Binding date}"  Grid.Row="1" Grid.Column="1"  HorizontalAlignment="Right" TextAlignment="Right"/>
                        <StackPanel Height="30"  Name="stkPanel01" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="1">
                            <TextBlock FontSize="25"  Name="txtNetw" Text="{Binding netw}"   HorizontalAlignment="Right" TextAlignment="Right"/>
                        </StackPanel>
                        <TextBlock FontSize="25" Name="txtVName" Text="{Binding vname}"  Grid.Row="2" Grid.Column="0" />

                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

    </Grid>

In my code file i will define the binding like this:

public class itemListForListBox
        {
            public string ebeln { get; set; }
            public string cname { get; set; }
            public string vname { get; set; }
            public string netw { get; set; }
            public string date { get; set; }

        }

And provide values like this:

void fillList()
        {
            List<itemListForListBox> itemListbox = new List<itemListForListBox>();
            itemListForListBox listItem;
            for (int i = 0; i < 5;i++ )
            {
                listItem = new itemListForListBox();

                listItem.ebeln = "Name "+i;
                listItem.date = "Date "+i;
                listItem.vname = "VName "+i;
                listItem.netw = "Amount "+ i;
                listItem.cname = "CName "+i;

                itemListbox.Add(listItem);
            }

            lstbNewOrders.ItemsSource = itemListbox;

        }

Hope this might help you. Thanks.

Upvotes: 1

ColinE
ColinE

Reputation: 70162

I take it you mean you have a collection of collections? In this case, you can nest your ItemsControls (or ListBox):

<ItemsControl ItemsSource={Binding Path=???}>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <!-- here is your nested itemscontrol -->
      <ItemsControl ItemsSource={Binding Path=???}>
        <ItemsControl.ItemTemplate>
          <DataTemplate>
            <!-- your content goes here -->
          </DataTemplate>
        <ItemsControl.ItemTemplate>
      </ItemsControl>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 0

Related Questions