WPF中ListView之间的文本分隔符

时间:2013-12-02 18:41:26

标签: wpf xaml listview

我正在尝试从ListView中获取以下内容:

Text | Text | Text

我已经通过以下

实现了垂直方向
<ItemsPanelTemplate><StackPanel Orientation="Horizontal"/></ItemsPanelTemplate>

每个部分(Text)都是绑定到MVVM中的字符串的TextBlock。 优选地,它们之间的线应该是规则的垂直条。

有关实现指定的垂直条的任何提示??

2 个答案:

答案 0 :(得分:2)

重复How can a separator be added between items in an ItemsControl,试试这个:

<ItemsControl Name="theListBox">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock x:Name="seperator" Text=" | "/>
                    <TextBlock Text="{Binding}"/>
                </StackPanel>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}" Value="{x:Null}">
                        <Setter Property="Visibility" TargetName="seperator" Value="Collapsed"/>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

答案 1 :(得分:0)

如果您已完成在两个项目之间显示分隔符,则可以使用转换器LastItemInContainerToVisibilityConverter,该转换器将绑定到制作separator collapsed for last itemvisible for all other items的分隔符的可见性。< / p>

假设您已使用Rectangle来显示项目之间的分隔 -

<ItemsControl.ItemTemplate>
   <DataTemplate>
      <StackPanel Orientation="Horizontal">
         <TextBlock Text="{Binding}"/>
         <Rectangle Visibility="{Binding RelativeSource={RelativeSource
                                   Mode=FindAncestor, AncestorType=ListViewItem},
                                  Converter={StaticResource 
                                   LastItemInContainerToVisibilityConverter}}"/>
       </StackPanel>
   </DataTemplate>
</ItemsControl.ItemTemplate>

如果转换器中的最后一项是

,那么转换器将返回
public class LastItemInContainerToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
                          CultureInfo culture)
    {
       DependencyObject item = (DependencyObject)value;
       ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);

       return (ic.ItemContainerGenerator.IndexFromContainer(item)
            == ic.Items.Count - 1) ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
                              CultureInfo culture)
    {
       throw new NotImplementedException();
    }
}