如何分隔在一个ItemsControl项目之间添加 [英] How can a separator be added between items in an ItemsControl

查看:211
本文介绍了如何分隔在一个ItemsControl项目之间添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个集合中的项目控制显示数字的列表。这样的项目是:1,2,3

当他们被渲染,我需要他们用逗号(或类似的东西)隔开。因此,上述3项是这样的:1,2,3

我如何分隔添加到单个项目,没有一件上涨对列表的末尾?

我不停留在使用一个ItemsControl,但是这就是我已经开始使用。


解决方案

 <的ItemsControl的ItemsSource ={结合数字}>
    < ItemsControl.ItemsPanel>
        < ItemsPanelTemplate>
            <! - 可以使用WrapPanel如果更适合您的方案 - >
            < StackPanel的方向=横向/>
        < / ItemsPanelTemplate>
    < /ItemsControl.ItemsPanel>
    < ItemsControl.ItemTemplate>
        <&DataTemplate的GT;
            < StackPanel的方向=横向>
                < TextBlock的X:名称=commaTextBlock文本=/>
                < TextBlock的文本={}绑定/>
            < / StackPanel的>
            < D​​ataTemplate.Triggers>
                < D​​ataTrigger绑定={绑定的RelativeSource = {的RelativeSource previousData}}VALUE ={X:空}>
                    < setter属性=能见度的TargetName =commaTextBlockVALUE =折叠/>
                < / DataTrigger>
            < /DataTemplate.Triggers>
        < / DataTemplate中>    < /ItemsControl.ItemTemplate>
< / ItemsControl的>

我来到了你的问题,因为我一直在寻找在Silverlight的解决方案,它不具有previous数据相对源。

I'm needing to display a list of numbers from a collection in an Items Control. So the items are: "1", "2", "3".

When they are rendered, I need them separated by a comma (or something similar). So the above 3 items would look like this: "1, 2, 3".

How can I add a separator to the individual items, without having one tacked on the end of the list?

I am not stuck on using an ItemsControl, but that's what I had started to use.

解决方案

<ItemsControl ItemsSource="{Binding Numbers}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <!-- could use a WrapPanel if more appropriate for your scenario -->
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock x:Name="commaTextBlock" Text=", "/>
                <TextBlock Text="{Binding .}"/>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                    <Setter Property="Visibility" TargetName="commaTextBlock" Value="Collapsed"/>
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>

    </ItemsControl.ItemTemplate>
</ItemsControl>

I arrived at your question because I was looking for a solution in Silverlight, which does not have a previous data relative source.

这篇关于如何分隔在一个ItemsControl项目之间添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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