I am facing problem with column width autoresizing.
I have code like that bellow.
<RadGridView ItemsSource="{Binding SomeObservableCollection}">
…
<RadGridView.Columns>
<GridViewDataColumn Header="First" DataMemberBinding="{Binding NameSource}" MinWidth="60"/>
<GridViewDataColumn Header="Second" DataMemberBinding="{Binding ValueSource}" MinWidth="145"/>
</RadGridView.Columns>
</RadGridView>
Columns auto resizing work well but only in one way. One time the width is increased never until new app start is decreased. I want width fit to content on each column all the time but never be thinner then MinWidth property value.
I have search very much but I have found no solution. For WPF there is not Autoresizing behaviour or Autoresize or BestFit method. Even if I set the Width property programmatically the ActualWidth property value never changes. I definitely call UpdateLayout on RadGridView.
Is there some simple solution?
Thank you very much.
6 Answers, 1 is accepted
Hi Jiri,
Can you try setting RadGridView's ColumnWidth property to SizeToCells and let me know about the result?
Regards,
Telerik by Progress
Hi Yoan,
I have tried it also before asking. In this case takes no visual effect because MinWidth is always longer than header text of both columns.
I am not sure that I can understand the exact problem. May I ask you to check the attached sample project and let me know how it differs from your requirement? Please notice that the MinWidth of the column is longer than the header and the SizeToCells property takes effect.
Regards,
Yoan
Telerik by Progress
Are you using NoXAML binaries? Please note that if you are using them and if there is a style targeting RadGridView, you will need to use the BasedOn attribute as demonstrated in the "Basing a Custom Style on a Theme Default Style" section of this help article.
Regards,
Yoan
Telerik by Progress
I have same problem (One time the width is increased never until new app start is decreased.)
I tried the header column assigned with new GridViewLength() but never changed column with
So, i used some fake as follows
public class ExtRadGridView : RadGridView
{
public void BestFitColumns()
{
this.ChildrenOfType<GridViewHeaderCell>()
.Where(f=>f.Column != null)
.Where(f=>f.Column.Width.IsAuto)
.ToList()
.ForEach(f =>
{
var thumb = f.ChildrenOfType<System.Windows.Controls.Primitives.Thumb>().LastOrDefault();
if (thumb != null)
{
f.Column.Width = 10;
f.Column.Width = new GridViewLength(1, GridViewLengthUnitType.Auto, 1, 1);
var args = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
RoutedEvent = Control.MouseDoubleClickEvent
};
thumb.RaiseEvent(args);
}
});
}
}
See GridViewHeaderCell Template
I hope it helps.