6

My UI:

<ListView Name="persons" SelectionChanged="persons_SelectionChanged">
        <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="auto"/>
                <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="auto"/>
            </GridView>
        </ListView.View>
    </ListView>

The Codebehind of my UI:

internal void Update(IEnumerable<Person> pers)
    {
        this.persons.ItemsSource = null;
        this.persons.ItemsSource = pers;
        UpdateLayout();
    }

My Entities:

class Person
{
   public string Name { get; set; }
   public int Age { get; set; }
}

The GridViewColumns have the width of the GridViewColumn-Header. Even if i call Update() with Persons with a long name. The Column does not get resized.

a) How can i resize the "Name"-column automatically (when i called Update) to the length of the longest Name, but not longer than a Value x (i want to specify a maximum width of a column)?

b) How can i specify that the "Age"-Column fills the space to the control´s end (so that the columns of the gridview uses the complete width of the control)?

H.B.
  • 166,899
  • 29
  • 327
  • 400
0xDEADBEEF
  • 3,401
  • 8
  • 37
  • 66

2 Answers2

8

GridView does not resize automatically.

To resize the columns you can

    foreach (GridViewColumn c in gv.Columns)
    {
        // Code below was found in GridViewColumnHeader.OnGripperDoubleClicked() event handler (using Reflector)
        // i.e. it is the same code that is executed when the gripper is double clicked
        // if (adjustAllColumns || App.StaticGabeLib.FieldDefsGrid[colNum].DispGrid)
        if (double.IsNaN(c.Width))
        {
            c.Width = c.ActualWidth;
        }
        c.Width = double.NaN;
     }  

As for sizing the last to fill area I do it with a converter. I don't think this converter does exactly what you need for this but it should get you started.

    <GridViewColumn Width="{Binding ElementName=lvCurDocFields, Path=ActualWidth, Converter={StaticResource widthConverter}, ConverterParameter=100}">


    [ValueConversion(typeof(double), typeof(double))]
    public class WidthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // value is the total width available
            double otherWidth;
            try
            {
                otherWidth = System.Convert.ToDouble(parameter);
            }
            catch
            {
                otherWidth = 100;
            }
            if (otherWidth < 0) otherWidth = 0;

            double width = (double)value - otherWidth;
            if (width < 0) width = 0;
            return width; // columnsCount;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

GridView is fast but it takes a bit of baby sitting.

paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • I know this is pretty old, but I want to highlight, how important the timing of the `ColumnWidth` fix is and that it really works well when applied correctly. Be sure that the GridView container is loaded and visible. I had a pretty nasty combination of a GridView being placed inside an expander which was in turn part of a virtualizing parent control. Meaning, a resize while being off-screen would yield zero width and the items could change when the template was recycled for different data. Now I'm kinda happy with my solution. – grek40 Nov 02 '16 at 18:05
1

I pulled this from How to autosize and right-align GridViewColumn data in WPF?

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

That help you at all?

Community
  • 1
  • 1
Chris Barlow
  • 3,274
  • 4
  • 31
  • 52