Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF Stretch a control to fill a ListView Column

Tags:

.net

listview

wpf

How do I make a control fill the whole cell in a ListView (with a GridView)? I've played around with various properties, but the control is always it's minimum size.

Here's my xaml.

<Window x:Class="ListViewUserControlTest.Window1"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns:local="clr-namespace:ListViewUserControlTest"   Title="Window1" Height="300" Width="300">     <Window.Resources>         <DataTemplate x:Key="UserControlCell">             <Rectangle Fill="Red" HorizontalAlignment="Stretch" MinWidth="10" MinHeight="10" />                       </DataTemplate>     </Window.Resources>     <Grid>                 <ListView Name="MyListView">             <ListView.View>                 <GridView>                     <GridViewColumn Width="30" Header="Col1" DisplayMemberBinding="{Binding Name}"  />                     <GridViewColumn Width="200" Header="Col2" CellTemplate="{StaticResource UserControlCell}"  />                 </GridView>             </ListView.View>         </ListView>     </Grid> 

Edit Sorry I changed the question because I had eliminated the user control as the source of the problem. It happens with any control.

like image 316
Ray Avatar asked Jan 29 '09 20:01

Ray


1 Answers

After narrowing down my question I was about to Google and find an answer here.

Basically for some reason the ListViewItems are set to align to the Left. Setting them to Stretch fixes this problem. This is done through a style like so:

<Style TargetType="ListViewItem">     <Setter Property="HorizontalContentAlignment" Value="Stretch" /> </Style> 

This unfortunately affects every column, but then you can set the contents of other columns to left, right, center as explained in the link.

like image 176
Ray Avatar answered Oct 15 '22 20:10

Ray