【问题标题】:Binding Listbox item source to a collection of collections in windows phone 7将列表框项目源绑定到 Windows Phone 7 中的集合集合
【发布时间】:2011-05-28 23:43:24
【问题描述】:

我正在尝试将 Listbox ItemSource 绑定到多个列表的集合。即

列出 PersonCollection

  • 列出人员

  • 列表集合

现在我需要显示这两个列表中的项目。我相信在 wpf 中您可以使用 HierarchicalDataTemplate,但不确定如何在 windows phone 7 中做到这一点。尝试使用 Blend 并生成以下数据模板。

<DataTemplate x:Key="PersonDataTemplate">
    <Grid>
        <StackPanel Margin="0,0,1,0" Orientation="Vertical" VerticalAlignment="Top">
            <TextBlock Margin="0,0,1,0" TextWrapping="Wrap" Text="{Binding Person[0].Name}" d:LayoutOverrides="Width"/>
            <TextBlock Margin="0,0,1,0" TextWrapping="Wrap" Text="{Binding Collection[0].Total}" d:LayoutOverrides="Width"/>
        </StackPanel>
    </Grid>
</DataTemplate> 

<ListBox Height="300" x:Name="personList" ItemsSource="{Binding PersonCollection}" Margin="10,0" ItemTemplate="{StaticResource PersonDataTemplate}"/>

还有其他方法可以做到这一点吗?我试图将 DataTemplate 中 Textbox 的 DataContext 设置为单个数组,但似乎没有用。除了确认 Windows Phone 7 不支持 HierarchicalDataTemplate 之外,在网上找不到任何类似的东西。

我有其他方法可以做,但没有优雅..

提前致谢。

问候

【问题讨论】:

    标签: xaml data-binding windows-phone-7 listbox


    【解决方案1】:

    我认为你的场景可以用两级 ListBoxes 而不是 Tree-Heirarchy 来解决。看看下面的技巧是否有效。现在您将在一个网格中并排看到您的两个内部集合,它们是另外两个 ItemsControls(或者您可以拥有 ListBoxes)

        <DataTemplate x:Key="PersonCollextionItem">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0.5*"/>
                    <ColumnDefinition Width="0.5*"/>
                </Grid.ColumnDefinitions>
                <ItemsControl ItemsSource="{Binding ListPerson}" ItemTemplate="{StaticResource Templ1}"  Grid.Column="0"/>
                <ItemsControl ItemsSource="{Binding ListCollection}" ItemTemplate="{StaticResource Templ2}" Grid.Column="1"/>
            </Grid>
        </DataTemplate>
    
       <DataTemplate x:Key="Templ1">            
           <TextBlock Margin="0,0,1,0" Text="{Binding Name}" />                
        </DataTemplate>
    
        <DataTemplate x:Key="Templ2">
             <TextBlock Margin="0,0,1,0" Text="{Binding Total}" />
        </DataTemplate>
    
    
        <ListBox Height="300" x:Name="personList" ItemsSource="{Binding PersonCollection}" Margin="10,0" ItemTemplate="{StaticResource PersonCollextionItem}"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 1970-01-01
      • 2012-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多