问题
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?
回答1:
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.
回答2:
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) });
回答3:
myGrid.Children.Clear();
remove all child controls
来源:https://stackoverflow.com/questions/16196118/remove-and-add-gridrow-definitions-in-c-sharp