WPF: GridView Column Width Calculator
10 Comments
Posted in Programming and WPF
The GridView in WPF isn’t bad, but is pretty basic compared to the one in ASP.NET. There’s isn’t really a decent method for setting column width, so I wrote this converter to calculate the width needed to fill the parent ListView.
If you call it with no parameters, it’ll take up the remaining width in the ListView:
// fills remaining width in the ListView <gridviewcolumn Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter}}"> </gridviewcolumn>
If you use an integer as a parameter, the value will act as the minimum width
// fills remaining width in the ListView, unless the remaining width is less than the parameter <gridviewcolumn Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter},ConverterParameter=200}"> </gridviewcolumn>
Or, you can specify a GridView type width with an asterisk, and the percentage width of the ListView will be returned
// calculates 30% of the ListView width <gridviewcolumn Width="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListView}},Converter={StaticResource WidthConverter},ConverterParameter=0.3*}"> </gridviewcolumn>
