Remove and add GridRow definitions in C#

点点圈 提交于 2019-12-11 13:11:36

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!