egfconnor
egfconnor

Reputation: 2647

Remove and add GridRow definitions in C#

I have a Grid with RowDefinitions defined in XAML that I need to change when going to snapped view in code, and so far I can only figure out how to remove them via:

RowDefinitionCollection defs = mainGrid.RowDefinitions;
defs.RemoveAt(0);
defs.RemoveAt(0);

Essentially I need to remove all definitions in snapped view (above code works) but then need to make the first row have a height of 140 and the second be "*" once it goes back into snapped. How would I add definitions with these characteristics?

Upvotes: 1

Views: 1197

Answers (3)

user3714810
user3714810

Reputation: 127

myGrid.Children.Clear();

remove all child controls

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48590

Simply

RowDefinitionCollection rdc = mainGrid.RowDefinitions;

rdc.Clear();

rdc.Add(new RowDefinition() { Height = new GridLength(140) });
rdc.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });

Upvotes: 3

Jim O'Neil
Jim O'Neil

Reputation: 23784

Try:

    RowDefinitionCollection defs = myGrid.RowDefinitions;
    defs.Add(new RowDefinition() { Height = new GridLength(140) });
    defs.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });

Alternatively, you could have two Grids and just modify the Visibility as part of the visual state, then you're not pulling a lot of tedious UI manipulation into your code. The built-in Visual Studio templates use this technique for snapped view.

Upvotes: 1

Related Questions