WPF DataGridColumn won't let me set “.width = double.NaN” for auto-sizing columns. “Value should not be infinity” error?

StackOverflow https://stackoverflow.com/questions/7656968

문제

I have a WPF DataGrid and I am setting up columns manually in code behind. I am trying to set the column size to Auto with the following code:

customBoundColumn.Width = double.NaN;

But then at run-time I get the following error:

Value should not be infinity. Parameter name: value

I read in many places that double.NaN is what is used to set the column size to auto. What am I doing wrong?

P.S. customBoundColumn is an instance of DataGridBoundColumn

도움이 되었습니까?

해결책

I believe you can simply use DataGridLength.Auto to set your width back to Auto

customBoundColumn.Width = DataGridLength.Auto;

다른 팁

In versions prior to .Net 4.0, the width of a datagrid column is set as

         customBoundColumn.Width = new DataGridLength(0, DataGridLengthUnitType.Auto);

I am not sure what is it in .Net 4.0.

Deafult width and height is Auto for all FrameworkElement-derived classes and so is for dataGrid columns. So, why do you need to set it explicitly in code behind?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top