ItemsControl有2个来源

时间:2014-02-07 14:23:08

标签: c# xaml data-binding windows-8 windows-store

我有以下课程:

public class agentes
{
    [PrimaryKey]
    public string id { get; set; }
    public string idContinente { get; set; }
    public string idCountry { get; set; }
    public string title { get; set; }
    public string desc { get; set; }
}

public class country
{
    [PrimaryKey]
    public string id { get; set; }
    public string idContinent { get; set; }
    public string title { get; set; }

}

我还有另外两个类,并且拥有每个类型,国家和代理商的对象列表。

我的列表中填充了数据库查询到sqlite数据库的结果,如下所示:

public List<agentes> GetAllAgents()
        {
            List<agentes> AllAgents = new List<agentes>();
            using (var db = new SQLite.SQLiteConnection(app.DBPath))
            {
                var query = db.Table<agentes>().OrderBy(c => c.idCountry);
                foreach (var _Agent in query)
                {
                    var Agente = new agentes()
                    {
                        desc = _Agent.desc,
                        id = _Agent.id,
                        idContinente = _Agent.idContinente,
                        idCountry = _Agent.idCountry,
                        title = _Agent.title,
                    };
                    AllAgents.Add(Agente);
                }
            }
            return AllAgents;
        }

现在,我打算做的是按国家/地区组织的代理商列表,因此它看起来像

enter image description here

我从这样的事情开始,但显然它需要什么

<ScrollViewer  Margin="30,30,0,30" Height="444">
                <ItemsControl Name="ListCountries">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding title}" Foreground="Red" />
                                <ItemsControl Name="AgentsByCountry" >
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <TextBlock Text="{Binding desc}" Foreground="Black" />
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </ScrollViewer>

任何人都有任何建议如何做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以使用LINQ按title进行分组,这将为IGrouping将成为您所在国家/地区的每个国家/地区创建Key

ListCountries.ItemsSource = GetAllAgents().GroupBy(a => a.title);

然后更改内部TextBlockItemsControl的绑定,如此

<ScrollViewer  Margin="30,30,0,30" Height="444">
    <ItemsControl Name="ListCountries">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Key}" Foreground="Red"/>
                    <ItemsControl ItemsSource="{Binding}" >
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding desc}" Foreground="Black" />
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ScrollViewer>
相关问题