在Silverlight中数字化手风琴到通用列表 [英] Databinding Accordion to Generic List in Silverlight

查看:186
本文介绍了在Silverlight中数字化手风琴到通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个包含两个属性(IdentityType和Name)的对象列表,格式如下:

Given a list of objects containing two properties (IdentityType and Name) in the format:

IdentityType | Name
A | One  
A | Two  
A | Three  
B | Four  
B | Five  
C | Six  

有没有办法声明性地数据绑定,所以手风琴显示如下?

Is there a way to declaratively databind that so the accordion displays like this?

A
- One
- Two
- Three
B
- Four
- Five
C
- Six

可以得到每个项目的面板标题,如下所示:

So far the best I can get is a panel header for each item, like so:

<toolkit:Accordion ItemsSource="{Binding Path=Identities}" Grid.Row="2" SelectionMode="ZeroOrMore">
        <toolkit:Accordion.ItemTemplate>
            <DataTemplate >
                <TextBlock Text="{Binding IdentityType, Converter={StaticResource EnumDescriptionConverter}}"/>
            </DataTemplate>
            </toolkit:Accordion.ItemTemplate>
            <toolkit:Accordion.ContentTemplate>
            <DataTemplate>
                <StackPanel Margin="5" Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" Foreground="White" />
                </StackPanel>
            </DataTemplate>
        </toolkit:Accordion.ContentTemplate>
    </toolkit:Accordion>

我是Silverlight的新手,所以我可能会丢失一些明显的东西,但是任何帮助都会很非常感谢!

I'm new to Silverlight so I could be missing something blindingly obvious, but any help would be very much appreciated!

推荐答案

您可以使用模型(头文件列表)和您的视图(标记)之间的视图模型)

You can do this with a view model inbetween your model (the initail list) and your view (the markup).


  • 使用标题和NameCollection创建视图模型类

  • 使用LINQ(或一个简单的foreach)将您现有的6个实体列表分别以3,2和1个名称的名称收录在3个名单中。

  • 将您的Accordions ItemsSource绑定到ViewModel对象

  • 将手风琴项目标题模板中的文本块绑定到您的标题属性

  • 将一个重复的项目控件(如ItemsControl)添加到您的内容您的手风琴项目的模板

  • 将重复的项目绑定到NamesCollection

  • Create a view model class with a Title and a NameCollection
  • Use LINQ (or a simple foreach) to translate your existing list of 6 entities to a list of 3 entites with 3, 2 and 1 Names in their name collection respectively.
  • Bind your Accordions ItemsSource to the collection of ViewModel objects.
  • Bind the text block in the your accordion items header template to your Title property
  • Add a repeating item control like ItemsControl to your content template of your accordion item
  • Bind your repeating item to the NamesCollection

假设您的模型是如下...

Assuming your model is as follows...

public class Model
{
    public string Title { get; set; }
    public string Name { get; set; }
}

您的View模型结构应该是...

Your View Model structure should be...

public class ViewModel
{
    public string Title { get; set; }
    public List<string> Names { get; set; }
}

public class DataContextClass
{
    public DataContextClass()
    {
        var modelData = new ModelData();

        var query = from m in modelData.ModelCollection
                    group m by m.Title
                    into vm select new ViewModel { Title = vm.Key, Names = vm.Select(x => x.Name).ToList() };
        ViewModelCollection = query.ToList();
    }

    public List<ViewModel> ViewModelCollection { get; set; }
}

然后,您的视图可以创建一个DataContextClass的实例,将其分配给它自己的DataContext属性,然后使用这个标记...

Then your view can create an instance of your DataContextClass, assign it to it's own DataContext property and then use this markup...

<layout:Accordion ItemsSource="{Binding Path=ViewModelDataInstance.ViewModelCollection}" >
    <layout:Accordion.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title}" />
        </DataTemplate>
    </layout:Accordion.ItemTemplate>
    <layout:Accordion.ContentTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding Path=Names}" />
        </DataTemplate>
    </layout:Accordion.ContentTemplate>
</layout:Accordion>

这篇关于在Silverlight中数字化手风琴到通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆