来自 Window Phone 7 中列表集合的绑定列表集合

发布于 2024-11-08 12:00:39 字数 53 浏览 0 评论 0原文

如何从 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娇纵 2024-11-15 12:00:39

首先,您的 Xaml 中有项目模板。
为其添加绑定。
在代码中定义该绑定属性。
为定义的属性分配值。

我的 Xaml 中有一个项目模板,如下所示:

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

在我的代码文件中,我将像这样定义绑定:

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

        }

并提供这样的值:

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;

        }

希望这可以帮助您。
谢谢。

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.

薯片软お妹 2024-11-15 12:00:39

您可以使用下面的代码,

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

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>
旧城烟雨 2024-11-15 12:00:39

我想你的意思是你有很多收藏品?在这种情况下,您可以嵌套 ItemsControls(或 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>

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>
强辩 2024-11-15 12:00:39

假设我们有一个 ListBox lstbx 和一个集合,假设

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

我们可以通过 Add() 将项目添加到集合中
Ex-

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

然后我们可以将集合直接分配给列表框的项目源
ex.

lstbx.ItemSource=listdata;

//确保如果您在集合的单个项目中存储多个变量,您应该为ListBox项目模板创建自定义数据模板。 !

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文