47

I am using ListView control instead of DataGrid in my WPF application. I want to give * width to my ListView.GridViewColumn, but whenever I am providing * width to ListView.GridViewColumn, it gives me a compile time error. Kindly suggest me how can I provide * width to ListView.GridViewColumn, so that ListView.GridViewColumn can automatically fill extra space when I maximize screen.

Any help on this will highly appreciated. Thanks

amiry jd
  • 27,021
  • 30
  • 116
  • 215
Yogesh
  • 829
  • 2
  • 8
  • 21

2 Answers2

84

Please try that solution:

<ListView>
    <ListView.View>
        <GridView>
            <GridViewColumn Header="column1" x:Name="col1"/>
            <!--Column that shall resize: Width is set to the Actual Width of the helper field defined below-->
            <GridViewColumn Header="column2" 
                            Width="{Binding ElementName=helperField, Path=ActualWidth}"/>
        </GridView>
    </ListView.View>
    Test Text
</ListView>

<!--This is the hidden helper Grid which does the resizing -->
<Grid Visibility="Hidden">
    <Grid.ColumnDefinitions>
        <!--Width is bound to width of the first GridViewColumn -->
        <ColumnDefinition Width="{Binding ElementName=col1, Path=ActualWidth}"/>
        <!--Width is set to "Fill"-->
        <ColumnDefinition Width="*"/>
        <!--Correction Width-->
        <ColumnDefinition Width="10"/>
    </Grid.ColumnDefinitions>
    <!--This is the hidden helper Field which is used to bind to, using the "Fill" column of the helper grid-->
    <Grid Grid.Column="1" x:Name="helperField"/>
</Grid>

You could also find some other solution at the following link:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3ee5696c-4f26-4e30-8891-0e2f95d69623/

amiry jd
  • 27,021
  • 30
  • 116
  • 215
Bilal Hashmi
  • 1,465
  • 13
  • 12
  • Thanks Bilal Hashmi. But in the above xaml approach where I should put line of code in my xaml page, so that it would effect ListView column. And using IValueConverter it works fine very first time when screen loads but when I maximize screen or resize screen it does not resize column width accordingly. – Yogesh Apr 25 '12 at 05:32
  • perhaps this could help http://stackoverflow.com/questions/560581/how-to-autosize-and-right-align-gridviewcolumn-data-in-wpf – Klaus78 Apr 25 '12 at 05:47
  • You could put it anywhere on that xaml page where you are showing your ListView. Lets say at Row=0 and Column=0 of the main grid of page. – Bilal Hashmi Apr 25 '12 at 09:52
  • 2
    Definitely a hack, but it is simple enough and works well enough that I like it. – JRadness May 15 '13 at 20:55
  • 1
    This is a very nice hack. The main problem I had with it is with the ListViews scrollbar in auto, the column width need to update automatically when the scrollbar appears and disappears. Also I don't like the "Correction Width" hack. My solution was to combine the above solution with [Q#9907017](http://stackoverflow.com/questions/9907017) and bind to "ViewportWidth" (not like in the answer) of the ListView's ScrollViewer. Note to activate the binding in the code behind after ListView was layout by hooking to LayoutUpdated (see [Q#4708039](http://stackoverflow.com/questions/4708039#18569270)) – Guss Oct 09 '13 at 15:32
  • Probably fine for when the number of columns is slow otherwise I fear this might have scaling issues –  Nov 18 '20 at 02:01
4

I posted my approach to this here which is a little different (but found it to be very reliable and allows percentage width columns https://stackoverflow.com/a/10526024/41211) as I tried the above and was finding my devenv.exe processing maxing out as it was constantly trying to re-evaluate my designer view with the above dynamic bindings.

Community
  • 1
  • 1
GONeale
  • 26,302
  • 21
  • 106
  • 149