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

RadGridView Column Header doesn't auto change width

1 Answer 227 Views
GridView
This is a migrated thread and some comments may be shown as answers.
runer
Top achievements
Rank 1
runer asked on 21 Feb 2019, 01:57 AM

When I change column header from longer to shorter one, the width not change

 

here is demo:

first a BindingData to transfer Header property to GridViewColumn Header property:

sealed class BindingData : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new BindingData { Data = this.Data };
    }
 
    public object Data
    {
        get { return (object)this.GetValue(DataProperty); }
        set { this.SetValue(DataProperty, value); }
    }
 
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register(
            "Data",
            typeof(object),
            typeof(BindingData));
}

 

ViewModel:

sealed class ViewModel : ViewModelBase
{
    private string header = "";
    public string Header
    {
        get { return this.header; }
        set
        {
            if (this.header != value)
            {
                this.header = value;
                NotifyPropertyChanged("Header");
            }
        }
    }
 
    public IEnumerable<string> HeaderSource
    {
        get
        {
            return Enumerable.Range(1, 3)
                .Select(item => "Header " + item)
                .Concat(Enumerable.Range(3, 5)
                    .Select(item => "Very Very Very Long Header " + item));
        }
    }
 
    public IEnumerable<string> RowSource
    {
        get
        {
            return Enumerable.Range(1, 16)
                .Select(item => "Data " + item);
        }
    }
}

 

View:

<StackPanel>
    <StackPanel.Resources>
        <local:BindingData
            x:Key="HeaderBindingData"
            Data="{Binding Header}"
            />
    </StackPanel.Resources>
     
    <ComboBox
        ItemsSource="{Binding HeaderSource}"
        SelectedValue="{Binding Header, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
        />
     
    <telerik:RadGridView ItemsSource="{Binding RowSource}">
        <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn
                DataMemberBinding="{Binding}"
                Header="{Binding Data, Source={StaticResource HeaderBindingData}}"
                />
        </telerik:RadGridView.Columns>
    </telerik:RadGridView>
</StackPanel>

 

Telerik.Windows.Controls.GridView.dll version is under 2014.2

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 25 Feb 2019, 11:14 AM
Hello Runer,

This behavior is expected. The column usually tries to expand in order to show its header. However, it doesn't shrink if the header is small. To alter this behavior you can reset the Width of the corresponding column when the header changes. For example, in your case you can use the SelectionChanged event of the ComboBox control. 
private void ComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    this.gridView.Columns[0].Width = new GridViewLength(1, GridViewLengthUnitType.Pixel);
    this.gridView.Columns[0].Width = new GridViewLength(1, GridViewLengthUnitType.Auto);
}
You can also check the attached project. I hope this helps.

Regards,
Martin Ivanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
runer
Top achievements
Rank 1
Answers by
Martin Ivanov
Telerik team
Share this question
or