在Silverlight中自定义绑定到Accordion控件

时间:2013-03-06 16:01:33

标签: silverlight binding collections accordion datacontext

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

客户端就像这样创建

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的项集合就变为只读。我也不想为公共分配创建单独的计划类型,因为公共分配实际上并不代表客户的计划。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

如何为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" });
    }
}
相关问题