Binding Grid.Width to sum of Column.width(s)

Sep 3, 2009 at 5:57 PM

here is my senario...

I allow my users to resize columns but they complain that when they resize them to be more narrow, an "unsightly" dummy placeholder column is displayed to the right.

I was talking to Samantha and she mentioned I could bind the width of my grid to the sum of the column widths.

I created a ValueConvertor which takes in DataGrid.Columns and bound it to the Width property of the datagrid.



                        <Binding RelativeSource="{RelativeSource Self}"


                        <Binding RelativeSource="{RelativeSource Self}"
                             Converter="{DPETValueConverters:WidthfromColumnSumConvertor}" >

All of my columns are specified in the XAML (not autogenerated)

In my convertor, I see that the DataGrid column object is passed in BUT the length is 0



    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

            int newWidth = 0;                       
            //make sure there is an incomming object
            if (value != null)
                //Loop through the columns and sum their actual widths

            return newWidth;





Any alternatives?

Any help is welcome... is there an attribute or behavior I am just missing??


Sep 8, 2009 at 1:58 PM

The sum of th actual widths are zero?  What is the code that you are using in that loop in the converter? 

Sep 8, 2009 at 2:14 PM

So I took a look a bit further and I think I understand now.  It's not the sum, but the column count is zero.  Is that what you're seeing?  Well if so, the reason is b/c the columns have not been set yet.  If you place the Width declaration after your columns declaration then you should see the complete set of columns.  Also, try summing the column.Width.DisplayWidth instead of the ActualWidth.