DCSIMG
WPF ListView Vertical Lines (Horizontal as Bonus) - Essential XAML

WPF ListView Vertical Lines (Horizontal as Bonus)

It all started half year ago, after the Japanese (customer of my customer) UI designer sent a photo of how a ListView should look like. First we though: "Piece of Cake, WPF...", then we saw the weird, annoying vertical-lines that stuck in the middle of nowhere... So we decided to provide a solution later :-)

Did you notice that neither WPF ListView (using a GridView as its view) nor custom grids such as Infragistics, Xceed, etc, provide an out-of-the-box vertical lines?

 

Download this code from here.

 

image

 

Starting by digging inside the complicated WPF ListView/GridView template and style, I didn't find any native option to draw my lines.

Why? Because the ListView/GridView is composed with several helper-controls which are decoupled from each other. In this case, the header part is rendered by the GridViewHeaderRowPresenter and the rows part is rendered by the ScrollContentPresenter.

 

To overcome this problem, without writing a custom GridViewHeaderRowPresenter I hooked into the ScrollContentPresenter by changing its content to Grid with ContentControl for the original content (rows), and ItemsControl with a Data Tenmplate to draw each column with a border.

 

<ScrollContentPresenter x:Name="PART_ScrollContentPresenter"
            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"                                            
            ContentTemplate="{TemplateBinding ContentTemplate}"
            KeyboardNavigation.DirectionalNavigation="Local"
            CanContentScroll="{TemplateBinding CanContentScroll}">

    <ScrollContentPresenter.Content>
        <Grid>
            <!-- Container of vertical and horizontal lines -->
            <ItemsControl Margin="3,0,0,0"
                          ItemsSource="{Binding Path=TemplatedParent.View.Columns,
                                        RelativeSource={RelativeSource TemplatedParent}}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border Width="{Binding Path=ActualWidth}"
                                BorderThickness="0,0,1,0"
                                BorderBrush="{DynamicResource verticalLineColor}" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                
                <!-- Fill background with horizontal lines -->
                <ItemsControl.Background>
                    <VisualBrush TileMode="Tile"
                                 Stretch="None"
                                 Viewport="{Binding Source={StaticResource columnHeight},
                                            Converter={StaticResource columnViewportConverter}}"
                                 ViewportUnits="Absolute">
                        <VisualBrush.Visual>
                            <StackPanel HorizontalAlignment="Stretch"
                                        VerticalAlignment="Stretch">
                                <!-- Add Rectangles here for more horizontal lines -->
                                <Rectangle Height="{DynamicResource columnHeight}"
                                           VerticalAlignment="Stretch"
                                           Fill="{DynamicResource horizontalLineColor1}"
                                           Width="1" />
                                <Rectangle Height="{DynamicResource columnHeight}"
                                           VerticalAlignment="Stretch"
                                           Fill="{DynamicResource horizontalLineColor2}"
                                           Width="1"  />
                            </StackPanel>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </ItemsControl.Background>        
            </ItemsControl>
            <ContentControl Content="{TemplateBinding Content}" />
        </Grid>
    </ScrollContentPresenter.Content>                                        
</ScrollContentPresenter>

 

As a bonus, I have added multi colors horizontal-lines all the way down (not only for items in the ListView) as a background brush, which is not the standard solution you are used to.

 

Enjoy by download this code from here.

Published Sunday, December 16, 2007 1:04 AM by Tomer Shamam

Comments

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Wednesday, August 20, 2008 5:19 PM by Scott

This solution does not work very well as if you add a lot of items to your listview, the items do not line with the background's.  Please update with the correct fix.

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Thursday, August 28, 2008 4:04 PM by Dan

I know this is an old entry, but I just love it when someone reads what you write and then demands you do something. Apparently, you owe them something now!

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Thursday, August 28, 2008 4:20 PM by Tomer Shamam

Hi Dan,

Completely agree with you :-)

But don't get it too serious. As for me, I just got use to it.

Cheers

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Monday, November 24, 2008 10:38 PM by Huy

Your example help me created the list view, that I was looking for.  

huydinhpham.blogspot.com/.../using-listvew-to-display-complex-data.html

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Wednesday, August 19, 2009 9:00 PM by Karol Deland

To correct the alignment problem add 2 in the Viewport converter to the result and change the margin 3,0,0,0 to 3,-1,0,0

return new Rect(0, 0, 1, (columnHeight * 2) + 2);

<ItemsControl Margin="3,-1,0,0"... >

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Friday, September 11, 2009 9:19 AM by Anee

Hi,

This link is really helpful to create a listview with styles.

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Friday, November 13, 2009 2:46 AM by biosys

Hello,

I just want to say: Than you! And than you very much. Your example is realy simple and very easy to understand. This is very good point to start with :)

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Friday, November 13, 2009 6:01 AM by Tomer Shamam

You welcome! It makes me happy to hear that :)

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Wednesday, January 27, 2010 7:56 AM by Uzi

Hi,

This is great work.

QUESTION

========

How I Change the background of Row on MouseOver

in this example?

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Friday, August 06, 2010 5:05 AM by Deepak

Hey,  I have followed the above code to create a horizontal and vertical grid lines. but I need to handle the mouse over event of the listview. I need to use trigger to trap the mouse over event so that I can disable it. And also upon row select i need to change the color of the selected row.

Thanks

Deepak

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Friday, August 20, 2010 11:39 AM by RenGuoqiang

The Performance is not good.I show registry items. It don't response on Loading

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Monday, January 17, 2011 10:00 AM by Yao.Guo

Hey, I can't show Horizontal Lines ? Why ?  I am crazying!

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Tuesday, March 01, 2011 4:55 AM by Tim

Hi , When I follow your solution , I found the virtualization for the listview failed . Do you have any I idea ?

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Sunday, June 19, 2011 3:42 PM by CatsFriends

Where can I read more about this?

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Sunday, June 19, 2011 9:16 PM by IntFashion

ok! theme revealed... thanks

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Thursday, August 04, 2011 4:45 AM by LL

This is a great work, but it doesn't work with VirtualizingStackPanel, is there a solution?

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Friday, July 13, 2012 12:11 PM by Sebastian

To make the list virtualizing, first take the ListView style from msdn.microsoft.com/.../ms788747.aspx into your view.

Then, add 'Name="ItemsPresenter"' to the ItemsPresenter control in the style.

Somehow, WPF does not find the "right" ItemsPresenter when you are using the ScrollContentPresenter above. It is found again after adding the name, and the VirtualizingStackPanel will actually be virtualizing.

# re: WPF ListView Vertical Lines (Horizontal as Bonus)

Thursday, November 08, 2012 1:55 PM by johnny

great stuff!

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above:
Powered by Community Server (Commercial Edition), by Telligent Systems