我需要在一个项控件中显示来自集合的数字列表。这些项目是: "1", "2", "3"
。
当它们被渲染时,我需要它们用逗号(或类似物)隔开。因此上述三个项目应该像这样:"1, 2, 3"
。
如何在不在列表末尾添加一个分隔符的情况下,给各个项目添加一个分隔符?
我不一定要使用项控件,但那是我开始使用的。
我需要在一个项控件中显示来自集合的数字列表。这些项目是: "1", "2", "3"
。
当它们被渲染时,我需要它们用逗号(或类似物)隔开。因此上述三个项目应该像这样:"1, 2, 3"
。
如何在不在列表末尾添加一个分隔符的情况下,给各个项目添加一个分隔符?
我不一定要使用项控件,但那是我开始使用的。
<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>
我之所以看到你的问题,是因为我在寻找Silverlight中没有相对数据源的解决方案。
当前被认可的答案为每个模板提供了一个XAML绑定错误,这让我担心会影响性能。相反,我使用AlternationIndex来隐藏第一个分隔符,如下所示。(受该答案的启发。)
<ItemsControl ItemsSource="{Binding Numbers}" AlternationCount="{Binding RelativeSource={RelativeSource Self}, Path=Items.Count}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="SeparatorTextBlock" Text=", "/>
<TextBlock Text="{Binding .}"/>
</StackPanel>
<DataTemplate.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Visibility" TargetName="SeparatorTextBlock" Value="Collapsed" />
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ControlTemplate />
,而其他解决方案则不然,这意味着它可以在没有数据绑定的情况下使用(其中您的项目在XAML中指定)。 简直太棒了。 - bokibeg如果需要更加通用的Silverlight兼容解决方案,可以从ItemsControl(SeperatedItemsControl)派生出一个控件。每个项目都包装在SeperatedItemsControlItem中,就像ListBox的ListBoxItem一样。SeperatedItemsControlItem的模板包含一个分隔符和一个ContentPresenter。集合中第一个元素的分隔符是隐藏的。您可以轻松修改此解决方案以创建项目之间的水平条形分隔符,这也是我创建它的原因。
MainWindow.xaml:
<Window x:Class="ItemsControlWithSeperator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ItemsControlWithSeperator"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<local:ViewModel x:Key="vm" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource vm}">
<local:SeperatedItemsControl ItemsSource="{Binding Data}">
<local:SeperatedItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</local:SeperatedItemsControl.ItemsPanel>
<local:SeperatedItemsControl.ItemContainerStyle>
<Style TargetType="local:SeperatedItemsControlItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:SeperatedItemsControlItem" >
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="seperator">,</TextBlock>
<ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</local:SeperatedItemsControl.ItemContainerStyle>
</local:SeperatedItemsControl>
</Grid>
C# 代码:
using System;
using System.Windows;
using System.Windows.Controls;
namespace ItemsControlWithSeperator
{
public class ViewModel
{
public string[] Data { get { return new[] { "Amy", "Bob", "Charlie" }; } }
}
public class SeperatedItemsControl : ItemsControl
{
public Style ItemContainerStyle
{
get { return (Style)base.GetValue(SeperatedItemsControl.ItemContainerStyleProperty); }
set { base.SetValue(SeperatedItemsControl.ItemContainerStyleProperty, value); }
}
public static readonly DependencyProperty ItemContainerStyleProperty =
DependencyProperty.Register("ItemContainerStyle", typeof(Style), typeof(SeperatedItemsControl), null);
protected override DependencyObject GetContainerForItemOverride()
{
return new SeperatedItemsControlItem();
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is SeperatedItemsControlItem;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
//begin code copied from ListBox class
if (object.ReferenceEquals(element, item))
{
return;
}
ContentPresenter contentPresenter = element as ContentPresenter;
ContentControl contentControl = null;
if (contentPresenter == null)
{
contentControl = (element as ContentControl);
if (contentControl == null)
{
return;
}
}
DataTemplate contentTemplate = null;
if (this.ItemTemplate != null && this.DisplayMemberPath != null)
{
throw new InvalidOperationException();
}
if (!(item is UIElement))
{
if (this.ItemTemplate != null)
{
contentTemplate = this.ItemTemplate;
}
}
if (contentPresenter != null)
{
contentPresenter.Content = item;
contentPresenter.ContentTemplate = contentTemplate;
}
else
{
contentControl.Content = item;
contentControl.ContentTemplate = contentTemplate;
}
if (ItemContainerStyle != null && contentControl.Style == null)
{
contentControl.Style = ItemContainerStyle;
}
//end code copied from ListBox class
if (this.Items.Count > 0)
{
if (object.ReferenceEquals(this.Items[0], item))
{
var container = element as SeperatedItemsControlItem;
container.IsFirstItem = true;
}
}
}
protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
base.OnItemsChanged(e);
if (Items.Count > 1)
{
var container = (ItemContainerGenerator.ContainerFromIndex(1) as SeperatedItemsControlItem);
if (container != null) container.IsFirstItem = false;
}
if (Items.Count > 0)
{
var container = (ItemContainerGenerator.ContainerFromIndex(0) as SeperatedItemsControlItem);
if (container != null) container.IsFirstItem = true;
}
}
}
public class SeperatedItemsControlItem : ContentControl
{
private bool isFirstItem;
public bool IsFirstItem
{
get { return isFirstItem; }
set
{
if (isFirstItem != value)
{
isFirstItem = value;
var seperator = this.GetTemplateChild("seperator") as FrameworkElement;
if (seperator != null)
{
seperator.Visibility = isFirstItem ? Visibility.Collapsed : Visibility.Visible;
}
}
}
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (IsFirstItem)
{
var seperator = this.GetTemplateChild("seperator") as FrameworkElement;
if (seperator != null)
{
seperator.Visibility = Visibility.Collapsed;
}
}
}
}
}
您还可以使用多种数据绑定方式来绑定ItemsControl.AlternationIndex和ItemsControl.Count,并将AlternationIndex与Count进行比较,以确定您是否为最后一项。
将AlternationIndex设置为足够大的值,以适应所有项目,然后创建一个LastItemConverter并编写一个类似于以下内容的Convert方法:
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var alterationCount = (int)values[0];
var itemCount = (int)values[1];
if (itemCount > 1)
{
return alterationCount == (itemCount - 1) ? Visibility.Collapsed : Visibility.Visible;
}
return Visibility.Collapsed;
}
我想我应该提供我最终使用的解决方案。
我最终将我的项目集合绑定到 TextBlock 的文本上,并使用值转换器将绑定的项目集合转换为格式化字符串。