Lazy Cow Projects

WPF C# ListView Column Width Auto & *

If you have worked with WPF before you will be familiar with the following code for a grid view “Width=’1*’”. This a great feature that expand the individual columns based on the grids actual width.

Unfortunately this capability is not available in ListView. So to enable this functionality I have created some behaviors that can simple be attached to your wpf control to allow easy and neat column sizing.

See this awesome tutorial on behaviors (http://development-guides.silverbaylabs.org/Video/Prism-Commands)

Example:

To generate the following view you can use the code below


<ListView HorizontalAlignment="Stretch"
          Behaviours:GridViewColumnResize.Enabled="True">
        <ListViewItem></ListViewItem>
        <ListView.View>
            <GridView>
                <GridViewColumn  Header="Column *"
                                   Behaviours:GridViewColumnResize.Width="*" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox HorizontalAlignment="Stretch" Text="Example1" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn  Header="Column 2*" 
                          Behaviours:GridViewColumnResize.Width="2*" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox HorizontalAlignment="Stretch" Text="Example1" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn  Header="Column Auto" Width="Auto"  >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox HorizontalAlignment="Stretch" Text="Example1" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn  Header="50 S"
                                   Behaviours:GridViewColumnResize.Width="50" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox HorizontalAlignment="Stretch" Text="E" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn  Header="50" Width="50" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox HorizontalAlignment="Stretch" Text="E" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
        </GridView>
        </ListView.View>
</ListView>

How to add it to your project:

Download the following source file. Include it into your project and add the behaviors to your existing WPF controls.

Use : Behaviours:GridViewColumnResize.Enabled=”True” on the ListView at the top to enable list re sizing.

Use: Behaviours:GridViewColumnResize.Width=”*1” on the individual GridViewColumns to set the column size


  1. sawdesh reblogged this from lazycowprojects
  2. lazycowprojects posted this
To Tumblr, Love PixelUnion