Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF : Extend last column of ListView's GridView

I have a ListView with a GridView with 3 columns. I want last column to take up remaining width of the ListView.

like image 933
THX-1138 Avatar asked May 26 '09 15:05

THX-1138


4 Answers

That can't be done with simple XAML, but there are some solutions out there. Check this out:

  • ListView Layout Manager
  • Star size of a ListView column
like image 120
gcores Avatar answered Nov 08 '22 22:11

gcores


quick & dirty

xaml:

<ListView SizeChanged="ListView_SizeChanged" Loaded="ListView_Loaded" >
    <ListView.View>
        <GridView>
            <GridViewColumn Header="col1" Width="100" />
            <GridViewColumn Header="col1" Width="Auto" />
            <GridViewColumn Header="col1" />
        </GridView>
    </ListView.View>
</ListView>

cs:

private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
{
    UpdateColumnsWidth(sender as ListView);
}

private void ListView_Loaded(object sender, RoutedEventArgs e)
{
    UpdateColumnsWidth(sender as ListView);
}

private void UpdateColumnsWidth(ListView listView)
{
    int autoFillColumnIndex = (listView.View as GridView).Columns.Count - 1;
    if (listView.ActualWidth == Double.NaN)
        listView.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
    double remainingSpace = listView.ActualWidth;
    for (int i = 0; i < (listView.View as GridView).Columns.Count; i++)
        if (i != autoFillColumnIndex)
            remainingSpace -= (listView.View as GridView).Columns[i].ActualWidth;
    (listView.View as GridView).Columns[autoFillColumnIndex].Width = remainingSpace >= 0 ? remainingSpace : 0;
}
like image 41
pr0gg3r Avatar answered Nov 08 '22 22:11

pr0gg3r


There is a way to do it using behavior pattern

<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>

See the following link for some examples and link to read more http://lazycowprojects.tumblr.com/post/7063214400/wpf-c-listview-column-width-auto

And to see the source code. Check out https://github.com/rolfwessels/lazycowprojects/tree/master/Wpf

like image 8
Rolf Wessels Avatar answered Nov 08 '22 22:11

Rolf Wessels


How About Using a Style

<Style x:Key="GridViewExtraStyle" TargetType="{x:Type GridViewColumnHeader}">
    <Setter Property="Background" Value="{x:Null}"/>
    <Setter Property="Foreground" Value="{x:Null}"/>
    <Setter Property="BorderBrush" Value="{x:Null}"/>
    <Setter Property="Width" Value="1000"/>
</Style>

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Abc"/>
            <GridViewColumn Header="" HeaderContainerStyle="{DynamicResource GridViewExtraStyle}"/>
        </GridView>
    </ListView.View>
</ListView>
like image 6
Pale Ale Avatar answered Nov 08 '22 22:11

Pale Ale