Vertical gridlines in ListView
I have a WPF ListView that I am using as a GridView. Is there a way to get the vertical gridlines in there?
The ListView is set to MinHeight, so I want the GridLine to go all the way to the bottom of the grid to fill the empty space.
This seems like a pretty tricky problem. Can you decide?
source to share
This is how I do it.
<ListView Grid.IsSharedSizeScope="True"
ItemsSource="{Binding Path=MyList,FallbackValue='12345'}" >
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*" SharedSizeGroup="col0"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*" SharedSizeGroup="col2"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Margin="10,0"
Text="{Binding Path=Name, FallbackValue='Name goes here'}"/>
<Border Grid.Column="1" Margin="0,-2"
BorderBrush="DarkGray"
BorderThickness="0,0,1,0" />
<TextBlock Grid.Column="2" Margin="10,0"
Text="{Binding Path=DateModified, FallbackValue='Date goes here'}"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Properties SharedSizeGroup
and Grid.IsSharedSizeScope
are key to this. The Margin="0,-2"
on property Border
guarantees that the dividing line will appear as a continuous vertical line. If you add a vertical margin to two text boxes, you will need to increase the negative vertical edge of the border.
This will take care of aligning things into two equally sized columns with a vertical line between them, but will not propagate to areas where there is no data. ListView might not be the best option if you want this functionality.
Adding some data to a property MyList
in the ViewModel gives this as a result:
Of course, a property MyList
must be a list or collection of a class with Name
and DateModified
as properties.
source to share