简体   繁体   English

有没有办法从一个 ObservableCollection 创建和填充 ListViews

[英]Is there a way to create and fill ListViews from one ObservableCollection

I have an observable collection based on these data:我有一个基于这些数据的可观察集合:

在此处输入图片说明

There may be more new data added, so the number of lines in my data can vary (sometimes 2 queries, sometimes 5, ...)可能会添加更多新数据,因此我的数据中的行数可能会有所不同(有时是 2 个查询,有时是 5 个,...)

Each line contains a query.每行包含一个查询。

I want to display each query result on a ListView or GridView:我想在 ListView 或 GridView 上显示每个查询结果:

        <StackPanel Grid.Row="1">
            <Label Content="{Binding Type1}"/>
            <Label Content="{Binding Definition1}"/>
            <ListView x:Name="ListRequete1" AlternationCount="2" ItemContainerStyle="{StaticResource alternatingStyle}"
                          ItemsSource="{Binding }" Margin="20" Height="170">
                <ListView.View>
                    <GridView x:Name="GridViewQuery1">

                   </GridView>
                </ListView.View>
            </ListView>
        </StackPanel>

Is it possible to do a method:是否可以做一个方法:

for each line of the ObservableCollection, I run the query, if the query result is not empty, I create a listView and display the data?对于 ObservableCollection 的每一行,我运行查询,如果查询结果不为空,我创建一个 listView 并显示数据?

Exemple of final result :最终结果示例:

在此处输入图片说明

At it's simplest your ListView displays the contents of your ObservableCollection<SQlQuery> that is in your ViewModel.在最简单的情况下,您的 ListView 会显示您的 ViewModel 中ObservableCollection<SQlQuery>的内容。 where在哪里

class SQLQuery
{
  string Query{get; private set;}
  string Description {get; private set;}
}

You then implement a command within your ViewModel that is triggered by your ListView ItemSelected or ItemTapped event.然后,您在 ViewModel 中实现一个由 ListView ItemSelected 或 ItemTapped 事件触发的命令。 This could be handled in your code behind or by binding.这可以在您的代码后面或通过绑定来处理。 eg例如

private void SQLQueryListView_OnItemTapped(object sender, ItemTappedEventArgs e)
{
    var param = (SQLQuery)e.Query;
    if (((ViewModel)BindingContext).RunQueryCommand.CanExecute(param))
    {
        ((ViewModel)BindingContext).RunQueryrCommand.Execute(param);
    }
}

The difficulty arises in displaying the resulting data if each query returns data of a different structure or type.如果每个查询返回不同结构或类型的数据,那么显示结果数据就会出现困难。 For that you may need to consider implementing a specific ContentView for each set of results (each of which contains a ListView) and set their visibility according to which Query is run (if only one to be displayed at any one time)为此,您可能需要考虑为每组结果(每个结果都包含一个 ListView)实现一个特定的 ContentView,并根据运行的 Query 设置它们的可见性(如果每次只显示一个)

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

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