Home > database >  So set grid rows/columns to default filling?
So set grid rows/columns to default filling?

Time:02-17

When I create grid with row or column, if I don't specify width/height, column/row adjust it's height/width to it's content like this:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Button Content="TestBtn"
                Height="50"
                Width="100"/>

    </Grid>

Row set it's height to 50.

Question: How to return row/column to that default behavior after custom setting the width/height in code? Like this:

    public void SetRowHeight(double height, bool setDefault)
    {
        if (setDefault)
        {
            Grid0Row1.DefaultHeightBehaivor = true;
        }
        else
        {
            Grid0Row1.Height = new GridLength(Height);
        }
    }

CodePudding user response:

For that, you need to use new GridLength() with empty parameters and code will look like this:

public void SetRowHeight(double height, bool setDefault)
{
    if (setDefault)
    {
        Grid0Row1.Height = new GridLength();
    }
    else
    {
        Grid0Row1.Height = new GridLength(height);
    }
}
  • Related