Silverlight 中通用列表的数据绑定折叠式

发布于 2024-10-19 07:39:48 字数 1178 浏览 0 评论 0原文

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

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

有没有一种方法可以声明式数据绑定,以便手风琴显示如下?

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

到目前为止,我能得到的最好的结果是每个项目都有一个面板标题,如下所示:

<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 的新手,所以我可能会错过一些非常明显的东西,但我们将非常感谢任何帮助!

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>

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-10-26 07:39:48

您可以使用模型(初始列表)和视图(标记)之间的视图模型来执行此操作。

  • 创建具有 Title 和 NameCollection 的视图模型类
  • 使用 LINQ(或简单的 foreach)将现有的 6 个实体列表转换为 3 个实体的列表,其名称集合中分别包含 3、2 和 1 个名称。
  • 将 Accordions ItemsSource 绑定到 ViewModel 对象的集合。
  • 将手风琴项目标题模板中的文本块绑定到 Title 属性
  • 将重复项目控件(如 ItemsControl)添加到手风琴项目的内容模板
  • 将重复项目绑定到 NamesCollection

假设您的模型如下...

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

您的视图模型结构应该是...

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 属性,然后使用此标记...

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

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

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

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

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>
和影子一齐双人舞 2024-10-26 07:39:48

您也可以使用元组代替。
代码变为:

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

    var query = from m in modelData.ModelCollection
                group m by m.Title
                into vm select Tuple.Create(vm.Key,  vm.Select(x => x.Name).ToList() };
    Collection = query.ToList();
}

public Tuple<string,List<string>> Collection { get; set; }

}

Xaml 变为:

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

我希望它有帮助

You can also use Tuple instead.
Code becomes :

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

    var query = from m in modelData.ModelCollection
                group m by m.Title
                into vm select Tuple.Create(vm.Key,  vm.Select(x => x.Name).ToList() };
    Collection = query.ToList();
}

public Tuple<string,List<string>> Collection { get; set; }

}

Xaml become :

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

I hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文