This is a migrated thread and some comments may be shown as answers.

Set System.Windows.GridLength.Width from Column.Width

3 Answers 268 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Joel Palmer
Top achievements
Rank 2
Joel Palmer asked on 11 Jun 2015, 07:30 PM

I have a GridView that has 3 columns.  However, the host Grid needs to be just the width of the 1st column.  I have bound the column's Width Property to my Model View as shown:

 

<telerik:GridViewDataColumn
    Header="Name"
    DataMemberBinding="{Binding Name}"
    Width="{Binding NameColumnWidth}"
    IsReadOnly="True" />

However, the Cell should resize only based on the content size so it is defined as SizeToCells:

 

private GridViewLength _nameColumnWidth =
    new GridViewLength(0, GridViewLengthUnitType.SizeToCells);
 
[Display(
    AutoGenerateField=false)]
[System.Xml.Serialization.XmlIgnore]
public GridViewLength NameColumnWidth
{
    get { return _nameColumnWidth; }
    set
    {
        if (_nameColumnWidth != value)
        {
            _nameColumnWidth = value;
            notifyPropertyChanged("NameColumnWidth");
        }
    }
}

How can I set the grid width to match the width of the name column's width?  Is there a converter that takes the AutoSize value from the column and converts it to Pixels?  Or, can I set the System.Windows.GridLength = NameColumnWidth?  Can I do this another way if I don't use SizeToCells?

3 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 16 Jun 2015, 03:00 PM
Hello Joel ,

May I suggest you an approach a little different from the one you have started working on, but a lot more easier. Note also, that with your approach you will not be able to get the correct value of the column width, as the UI is not rendered yet.

So, just subsrcibe to the DataLoaded event of RadGridView, and by using a Dispatcher set the width of the Grid container:
void clubsGrid_DataLoaded(object sender, EventArgs e)
{
    Dispatcher.BeginInvoke(DispatcherPriority.Render,
        new Action(() =>
    {
        this.container.Width =
            this.clubsGrid.Columns[0].ActualWidth;
    }));
}

Can you please try it out and let me know if this visual appearance of RadGridView is what you were trying to achieve?

Best Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Joel Palmer
Top achievements
Rank 2
answered on 16 Jun 2015, 04:08 PM
Actually, I don't see Columns[0].ActualWidth changing.  It may be because GridViewLengthUnitType.SizeToCells may treat this differently than using the pixels.
0
Stefan
Telerik team
answered on 17 Jun 2015, 03:51 PM
Hello Joel ,

Can you confirm using a Dispatcher, as suggested in my previous post? However, I am attaching a sample project, in which the ActualWidth value of the columns is generated and properly set to the Grid container Width.

Please check it out and let me know if I can guide you any further.

Best Regards,
Stefan X1
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Joel Palmer
Top achievements
Rank 2
Answers by
Stefan
Telerik team
Joel Palmer
Top achievements
Rank 2
Share this question
or