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

ColumnWidthChangedEventArgs Width = 1?

9 Answers 75 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 14 Jun 2017, 02:44 PM

I'm handling the ColumnWidthChanged event and saving the width of each column in a config file for reuse the next time my application is run. If I drag the column to a different size the NewWidth property is the actual width I've chosen. If I double-click the column edge to auto-size it, the NewWidth property is set to "Auto" and the value is "1".

Why can't I see what the actual new width is? My gut says this is counter-intuitive, but Telerik generally has a good reason for this sort of thing.

Any input as to how I can get the actual width so I can save it?

9 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 19 Jun 2017, 10:13 AM
Hello David,

You can check this forum thread where this question had already been discussed.

Regards,
Yoan
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
David
Top achievements
Rank 1
answered on 19 Jun 2017, 07:05 PM

Hey Yoan,

I tried what the other forum post suggested and it's still not working. 

Can you confirm that the following code needs to be placed within the ColumnWidthChanged event handler? If so, when I step through the method that is being invoked, e.Column.ActualWidth is always equal to "2". 

if(e.NewWidth.IsAuto)
{
      this.radGridView.Dispatcher.BeginInvoke((Action)(()=>YourMethodHere(e.Column.ActualWidth)));
}
0
Yoan
Telerik team
answered on 20 Jun 2017, 02:23 PM
Hello,

Yes, you can use the ColumnWidthChanged event. I have tested this in a sample project and it works as expected. Can you confirm how it works at your side?

Regards,
Yoan
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
David
Top achievements
Rank 1
answered on 28 Jun 2017, 03:57 PM
On my side it works only half the time. Some times when the column edge is double-clicked I get an actual width value. Other times (when I double click the same column edge) I get the value "2". At first I thought it was dependent on whether or not the column adjacent was formatted with TextWrapping, but that doesn't seem to be the case.
0
David
Top achievements
Rank 1
answered on 28 Jun 2017, 04:00 PM
I think I have the pattern sorted out. It seems that the first time the column edge is double clicked, I get a width of 2 every single time. If I double click the column a second time, I get an actual width. Why the change?
0
David
Top achievements
Rank 1
answered on 28 Jun 2017, 05:03 PM

Sorry to spam this topic. I managed to get your sample project to reproduce the issue by doing the following:

In the MainWindow.xaml.cs file, change a column width in the constructor after InitializeComponent() is called. My constructor looks like this:

public MainWindow()
{
    InitializeComponent();
    clubsGrid.Columns[ 2 ].Width = 130;
}

When you double click the column header to get the width, the value returned by the ActualWidth property will be equal to the minimum column width (20 by default, I had mine set to 2).

0
Yoan
Telerik team
answered on 30 Jun 2017, 12:49 PM
Hello David,

You can try setting a priority of the Dispatcher like so:
this.clubsGrid.Dispatcher.BeginInvoke((Action)(() => Debug.WriteLine(e.Column.ActualWidth)),DispatcherPriority.Background);

It works in the sample project. Can you try it on your side?

Regards,
Yoan
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which you to write beautiful native mobile apps using a single shared C# codebase.
0
David
Top achievements
Rank 1
answered on 30 Jun 2017, 01:52 PM

It works! 

Is there any chance you could tell me why the priority needed to be set to Background? I spent three days trying to figure this out. I'm curious what I missed.

0
Yoan
Telerik team
answered on 04 Jul 2017, 07:14 AM
Hello David,

It seems that this is a timing issue. Most probably the actual width of the column is not reevaluated when the ColumnWidthChanged is invoked. You can get it working by delaying the call to e.Column.ActualWidth. The easiest way to do that is to use a dispatcher with priority set.

Regards,
Yoan
Progress Telerik
Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Tags
GridView
Asked by
David
Top achievements
Rank 1
Answers by
Yoan
Telerik team
David
Top achievements
Rank 1
Share this question
or