【问题标题】:Custom binding to Accordion control in Silverlight在 Silverlight 中自定义绑定到 Accordion 控件
【发布时间】:2013-02-21 12:40:49
【问题描述】:

有没有办法将集合绑定到 Silverlight 中的 Accordion 控件,但其中一个 Accordion 项是该集合共有的项的列表。例如,我有几种类型:Client、PlanCollection、Plan、AllocationCollection、Allocation。每个客户都有一个或多个计划,每个计划都有一个或多个分配。某些分配对所有计划都是通用的。公共分配本身包含在客户端计划集合的分配集合属性中。这里有一些示例代码来说明。

一个客户端是这样创建的

Client c = new Client() { Name = "Acme Company" };

计划的分配可以这样访问

c.Plans["Acme DB Plan"].Allocations

单个分配会像这样访问

Allocation first = c.Plans["Acme DB Plan"].Allocations[0];

一个计划的公共分配可以这样访问

c.Plans.CommonAllocations;

还有一个像这样的普通分配

Allocation firstCommon = c.Plans.CommonAllocations[0];

Accordion 中的每个标题都是一个计划名称,每个标题都将展开以显示计划中的分配。我还需要一个名为“Common Allocations”的单独标题,它可以展开以显示所有计划共有的分配。我似乎无法找到一种方法来做到这一点。我可以将计划正确地绑定到 Accordion 的 ItemsSource 属性,但是我不能将公共分配添加为单独的项目,因为一旦绑定了计划,Accordion 的项目集合就变为只读的。我也不想为公共分配创建单独类型的计划,因为公共分配实际上并不代表客户的计划。任何帮助将不胜感激。

【问题讨论】:

    标签: silverlight binding collections accordion datacontext


    【解决方案1】:

    如何为您的 Accordian 的 ItemsSource 创建一个分配集合。

    像这样创建集合:

    IEnumerable<Allocation> GetAllAllocations(Client c)
    {
        foreach (var plan in c.Plans)
        {
            yield return plan.Allocations;
        }
    
        yield return c.Plans.CommonAllocations;
    }
    

    如果需要,可以将其作为绑定属性公开:

    public IEnumerable<Allocation> AllAllocations
    {
        get
        {
            return GetAllAllocations(new Client() { Name = "Acme Company" });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 2011-03-20
      • 2011-10-13
      • 1970-01-01
      • 2011-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多