简体   繁体   English

UWP-将多个网格视图绑定到列表列表

[英]UWP - binding multiple grid views to a list of lists

I'm a little confused on how to accomplish this. 我对如何做到这一点有些困惑。 Here is the situation. 这是情况。 I have a collection of objects themselves have a collection of objects. 我有一个对象集合,本身也有一个对象集合。 I want to create a new grid view for every object in the first collection that would use the collection from the second object as its source. 我想为第一个集合中的每个对象创建一个新的网格视图,该视图将使用第二个对象中的集合作为其源。

For example. 例如。 In my view model I have something like this 在我的视图模型中,我有这样的东西

public ObservableCollection<ApiResponse> NewsStories

where ApiResponse is defined as something like 其中ApiResponse被定义为类似

public class ApiResponse
{
    public List<Article> Articles { get; set; }
}

My xaml before I made this a list of lists was something like 在将清单设为清单之前,我的xaml就像

<GridView ItemsSource="{Binding Articles}"
        ItemTemplate="{StaticResource GridViewTemplate }"
        IsItemClickEnabled="False"
        IsSwipeEnabled="False"
        CanDragItems="False"
        SelectionMode="Single"
        />

I was able to bind to just a single api response and see the grid view display all the articles from that response. 我能够仅绑定到一个api响应,并看到网格视图显示了该响应的所有文章。 But now that I'm going to have a list of lists I'm not really sure how to proceed. 但是,既然我要列出列表,则我不确定如何继续。 I tried enclosing this in a list view, to no avail. 我试图将其包含在列表视图中,无济于事。

I think I might have figured it out. 我想我可能已经知道了。 First in my xaml I have something like 首先在我的xaml中,我有类似

<GridView
        ItemsSource="{Binding NewsStories}"
        IsItemClickEnabled="False"
        CanDragItems="False"
        SelectionMode="None"
        ItemTemplate="{StaticResource ImageTextTemplate }">
        <GridView.ItemsPanel>
            <ItemsPanelTemplate>
                <ItemsWrapGrid Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </GridView.ItemsPanel>
    </GridView>

And in my page resources then I have 然后在我的页面资源中

<Page.Resources>
    <DataTemplate x:Key="ArticleTemplate"  x:DataType="data:Article">           
            <StackPanel Orientation="Vertical" Width="180" Height="240" >
             <!-- stuff here -->
            </StackPanel>
        </StackPanel>

    </DataTemplate>

    <DataTemplate x:Key="ImageTextTemplate" x:DataType="data:ApiResponse">          
            <GridView ItemsSource="{Binding Articles}"
                ItemTemplate="{StaticResource ArticleTemplate}"                    
                Header="{Binding Source}" />
    </DataTemplate>

</Page.Resources>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM