wpf dynamically set the row column height in wpf grid Edit

Pooran Prasad Rajanna | 09 September 2020 | 811

we have to use System.Windows.GridLength class to set row or column height in WPF grid

For example, you define grid-like below

<Grid  Margin="10,10,10,10" >
            <Grid.RowDefinitions>
                <RowDefinition Name="grdFirst" Height="30"/>
                <RowDefinition Name="grdSecond" Height="150"/>
                <RowDefinition Name="grdThird" Height="0"/>
            </Grid.RowDefinitions>
            ….
</Grid>


On some condition, you want to hide, show grids. Below is the code to do it programmatically

grdFirst.Height = new GridLength(0, GridUnitType.Pixel); grdSecond.Height = new GridLength(0, GridUnitType.Pixel); grdThird.Height = new GridLength(180, GridUnitType.Pixel);

You have to use GridUnitType.Pixel if you have done so in the XAML. Else use GridUnitType.Star in case you have defined the heights in percentages like 0.30*