运行时绑定到ListView.ItemTemplate中的项目

时间:2014-10-30 06:58:09

标签: winrt-xaml win-universal-app

我试图在ListView.ItemTemplate的DataTemplate中绑定一个项目。此ListView位于Grid中,如下所示:

    <Grid x:Name="catPageGrid">
    <Grid.ChildrenTransitions>
        <TransitionCollection>
            <EntranceThemeTransition />
        </TransitionCollection>
    </Grid.ChildrenTransitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <!--  Title Panel  -->
    <StackPanel Grid.Row="0" Margin="19,0,0,0">
        <TextBlock x:Uid="Header"
                   Margin="0,12,0,0"
                   Style="{ThemeResource TitleTextBlockStyle}"
                   Text="category page" />
        <TextBlock Margin="0,-6.5,0,26.5"
                   CharacterSpacing="{ThemeResource PivotHeaderItemCharacterSpacing}"
                   Style="{ThemeResource HeaderTextBlockStyle}"
                   Text="{Binding}"
                   TextWrapping="NoWrap" />
    </StackPanel>

    <ListView x:Name="lvAmenityItem"
              Grid.Row="1"
              Margin="19,0,0,0"
              AutomationProperties.AutomationId="ItemListView"
              AutomationProperties.Name="Items In Group"
              IsItemClickEnabled="True"
              IsSwipeEnabled="false"
              ItemClick="ItemView_ItemClick"
              ItemsSource="{Binding}"
              SelectionMode="None"
              TabIndex="1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid x:Name="grAmenityItem">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>

                    <Border Width="79"
                            Height="79"
                            Margin="0,9.5,0,0"
                            Background="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}">
                        <Image AutomationProperties.Name="{Binding}"
                               Source="{Binding}"
                               Stretch="UniformToFill" />
                    </Border>
                    <StackPanel Grid.Column="1"
                                Margin="14.5,0,0,0"
                                VerticalAlignment="Top">
                        <TextBlock Style="{ThemeResource ListViewItemTextBlockStyle}" Text="{Binding}" />
                    </StackPanel>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

我在网格上使用了运行时绑定:

    private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        var group = TestDataSource.GetAmenityGroup((string)e.NavigationParameter);
        catPageGrid.DataContext = group;

        //lvAmenityItem.DataContext = TestDataSource.GetAmenityItem((string)e.NavigationParameter);
    }

我的问题是我在运行后没有看到带有项目的网格。

我是通过默认的Hub项目模板完成的。我成功地以同样的方式绑定到Hub(显示&#34;第一级&#34;信息(即AmenityGroup有AmenityItem(s)))

我在这里做错了吗?或者我不能这样做吗?

感谢您的时间:D

修改

以下代码仅显示一个项目......作为开始

Binding binding = new Binding() { Source = group.AmenityItems };
lvAmenityItem.SetBinding(ListView.DataContextProperty, binding);

1 个答案:

答案 0 :(得分:0)

好的添加Binding已经成功了

刚刚将Text绑定更改为:                                                                       

Namegroup中返回的实体类中的变量。