none
ListView显示水平和垂直分割线 RRS feed

答案

  • Hi,

    参考下面的代码:

     public class GridViewRowPresenterWithGridLines : GridViewRowPresenter
        {
            private static readonly Style DefaultSeparatorStyle;
            public static readonly DependencyProperty SeparatorStyleProperty;
            private readonly List<FrameworkElement> _lines = new List<FrameworkElement>();
    
            static GridViewRowPresenterWithGridLines()
            {
                DefaultSeparatorStyle = new Style(typeof(Rectangle));
                DefaultSeparatorStyle.Setters.Add(new Setter(Shape.FillProperty, SystemColors.ControlLightBrush));
                SeparatorStyleProperty = DependencyProperty.Register("SeparatorStyle", typeof(Style), typeof(GridViewRowPresenterWithGridLines),
                                                                        new UIPropertyMetadata(DefaultSeparatorStyle, SeparatorStyleChanged));
            }
    
            public Style SeparatorStyle
            {
                get { return (Style)GetValue(SeparatorStyleProperty); }
                set { SetValue(SeparatorStyleProperty, value); }
            }
    
            private IEnumerable<FrameworkElement> Children
            {
                get { return LogicalTreeHelper.GetChildren(this).OfType<FrameworkElement>(); }
            }
    
            private static void SeparatorStyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var presenter = (GridViewRowPresenterWithGridLines)d;
                var style = (Style)e.NewValue;
                foreach (FrameworkElement line in presenter._lines)
                {
                    line.Style = style;
                }
            }
    
            protected override Size ArrangeOverride(Size arrangeSize)
            {
                var size = base.ArrangeOverride(arrangeSize);
                var children = Children.ToList();
                EnsureLines(children.Count);
                for (var i = 0; i < _lines.Count; i++)
                {
                    var child = children[i];
                    var x = child.TransformToAncestor(this).Transform(new Point(child.ActualWidth, 0)).X + child.Margin.Right;
                    var rect = new Rect(x, -Margin.Top, 1, size.Height + Margin.Top + Margin.Bottom);
                    var line = _lines[i];
                    line.Measure(rect.Size);
                    line.Arrange(rect);
                }
                return size;
            }
    
            private void EnsureLines(int count)
            {
                count = count - _lines.Count;
                for (var i = 0; i < count; i++)
                {
                    var line = (FrameworkElement)Activator.CreateInstance(SeparatorStyle.TargetType);
                    line = new Rectangle { Fill = Brushes.LightGray };
                    line.Style = SeparatorStyle;
                    AddVisualChild(line);
                    _lines.Add(line);
                }
            }
    
            protected override int VisualChildrenCount
            {
                get { return base.VisualChildrenCount + _lines.Count; }
            }
    
            protected override Visual GetVisualChild(int index)
            {
                var count = base.VisualChildrenCount;
                return index < count ? base.GetVisualChild(index) : _lines[index - count];
            }
        }

    Demo:

    <Window x:Class="wpfAppDemo.wpfListView.sample4"
            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:wpfAppDemo.wpfListView"
            mc:Ignorable="d"   
            Title="sample4" Height="300" Width="300">
        <Window.Resources>
            <XmlDataProvider x:Key="People">
                <x:XData>
                    <People xmlns="">
                        <Person>
                            <Name>Peter</Name>
                            <Surname>Black</Surname>
                            <Age>25</Age>
                        </Person>
                        <Person>
                            <Name>John</Name>
                            <Surname>Smith</Surname>
                            <Age>35</Age>
                        </Person>
                    </People>
                </x:XData>
            </XmlDataProvider>
             </Window.Resources>
        <Grid>
            <ListView Name="PeopleList" ItemsSource="{Binding Source={StaticResource People},XPath=/People/Person}">
                <ListView.ItemContainerStyle>
                    <Style TargetType="{x:Type ListViewItem}">
                        <Setter Property="Margin" Value="2,0,0,0"/>
                        <Setter Property="Padding" Value="0,2"/>
                        <Setter Property="BorderBrush" Value="LightGray"/>
                        <Setter Property="BorderThickness" Value="0,0,0,1"/>
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ListViewItem}">
                                    <Border BorderBrush="{TemplateBinding BorderBrush}" 
                                            BorderThickness="{TemplateBinding BorderThickness}" 
                                            Background="{TemplateBinding Background}">
                                        <local:GridViewRowPresenterWithGridLines 
                                            Columns="{TemplateBinding GridView.ColumnCollection}"
                                            Margin="{TemplateBinding Padding}" />
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.View>
                    <GridView>
                        <GridViewColumn Width="60" Header="Name" DisplayMemberBinding="{Binding XPath=Name}"/>
                        <GridViewColumn Width="80" Header="Surname" DisplayMemberBinding="{Binding XPath=Surname}"/>
                        <GridViewColumn Width="50" Header="Age" DisplayMemberBinding="{Binding XPath=Age}"/>
                    </GridView>
                </ListView.View>
            </ListView>
        </Grid>
    </Window>
    

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 oneonce 2017年8月16日 6:15
    2017年8月16日 5:46
    版主