简体   繁体   中英

DataGrid auto sizing in WPF

I am dynamically creating data grids in a tab window in WPF. I am having an issue with the data grid sizes, that are within the tabControl.

This is not an issue of the size of data within the data grid but the size of data grid itself.

I am looking to set the height and width to auto. This is simple in XAML but I am struggling to find any information to do it in the code which I need to do..

I have tired double.NaN but this does not seem to work....

Any help would be amazing!

I am using c#.

尝试

Height = new GridLength(1, GridUnitType.Auto);

The problem is probably the StackPanel. It won't give the controls inside of it any more space than they "need" in the direction of it's orientation (so a vertical stack panel will only give the minimum required vertical space to each element). Try replacing it with a Grid or DockPanel.

In other words - controls in a vertical StackPanel will NOT expand to fill available vertical space, similarly for a horizontal StackPanel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM