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

4 posts, 0 answers
  1. Joel Palmer
    Joel Palmer avatar
    175 posts
    Member since:
    May 2009

    Posted 11 Jun 2015 Link to this post

    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?

  2. Stefan
    Admin
    Stefan avatar
    1073 posts

    Posted 16 Jun 2015 Link to this post

    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
  3. Joel Palmer
    Joel Palmer avatar
    175 posts
    Member since:
    May 2009

    Posted 16 Jun 2015 in reply to Stefan Link to this post

    Actually, I don't see Columns[0].ActualWidth changing.  It may be because GridViewLengthUnitType.SizeToCells may treat this differently than using the pixels.
  4. Stefan
    Admin
    Stefan avatar
    1073 posts

    Posted 17 Jun 2015 Link to this post

    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
Back to Top