tags:

views:

304

answers:

3

How could I set the value of the Height property of a WPF control in C# code to "Auto"?

              <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>

I want to reproduce this behavior in the code behind. Any ideas?

Thanks in advance

+2  A: 

Perhaps this link will help you.

At times, you may want to programmatically set the Height or Width of a WPF element to Auto in code. To do this, just use the Double.NaN (Not a Number) value.

For example, in C#:

this.txtName.Width = Double.NaN;

Zach Johnson
A: 

It would look like this:

for (int i = 0; i < 9; i++)
            MyGrid.RowDefinitions.Insert(i,
                                            i%2 != 0
                                                ? new RowDefinition() {Height = new GridLength(0, GridUnitType.Auto)}
                                                : new RowDefinition());
Karim
A: 

You can use


RowDefinition rd = new RowDefinition();  
rd.Height = GridLength.Auto;  
ContentGrid.RowDefinitions.Add(rd);

Nimrod Shory