简体   繁体   中英

WPF Grid won’t resize to fit nested content

I'm programmatically inserting grids into grids, for the first nested, it will work perfect. But then from the second it stops resizing to fit for the content.

The only defaults I override are the MinWidth and the MinHeight.

EDIT: Each time I'm creating a grid, I add a stackpanel (with lable inside) to each cell. Then I Insert a nested grid to that stack panel.

在此处输入图片说明

As seen above, the grid that's being marked with the green thing, dose not affect the mainGrid size.

Thanks

Apparently the grid restricts it's maximum size, I don't know for what purpose.

Hopefully that won't back fire at me later. but for now, all I had to do was create and use a custom grid with unlimited 'resize room'.

public class CustomGrid : Grid
{
    protected override Size MeasureOverride(Size availableSize)
    {
        availableSize = new Size(availableSize.Width + double.MaxValue, availableSize.Height + double.MaxValue);
        return base.MeasureOverride(availableSize);
    }
}

There are probably more legitimate solutions to this problem, but for the moment that's all I need.

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