【问题标题】:ItemControl with 2 sources具有 2 个源的 ItemsControl
【发布时间】:2014-03-05 00:09:02
【问题描述】:

我有以下课程:

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; }

}

我还有 2 个其他类,并且有一个列表,其中包含每种类型、国家和代理的对象。

我的列表中填充了从数据库查询到 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;
        }

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

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

<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>

有人有什么建议吗?

【问题讨论】:

  • 您遇到了哪一点问题?分组?
  • 是的,我不知道如何显示代理,以某种方式将它们与国家/地区分组,我可以在一个列表中显示所有代理,但这并不是我想要的最终结果就像我添加的图像一样

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


【解决方案1】:

您可以使用 LINQ 按title 分组,这将为每个国家/地区创建IGroupingKey 将是您的国家/地区:

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>

【讨论】:

  • 还没有工作,它只是打印国家的名称。请注意,country 类中的 title 和 agents 类中的 title 是不同的东西,它们是每个对象的名称,agents 对象称为 John,Country 对象称为 Spain。不知道在我的初始帖子上是否清楚
  • 您是否在内部ItemsControl 上添加了ItemsSource="{Binding}"
  • 是的: 像这样
  • 我将 groupby 更改为 GroupBy(a => a.idCountry);现在似乎对它们进行了分组,因为 idCountry 是与它们相关的东西,但是在应用程序中,它们的标题显示为数字,因为我们使用的是 idCountry ... hmmm
  • 您需要加入country 表,从agenttitle 中选择title 并按国家名称而不是国家ID 分组
猜你喜欢
  • 2017-10-15
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多