在 C# 中删除和添加 GridRow 定义 [英] Remove and add GridRow definitions in C#

查看:18
本文介绍了在 C# 中删除和添加 GridRow 定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在 XAML 中定义了 RowDefinitions 的网格,我需要在代码中进行捕捉视图时更改它,到目前为止,我只能通过以下方式弄清楚如何删除它们:

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);

本质上,我需要删除对齐视图中的所有定义(以上代码有效),但随后需要使第一行的高度为 140,而第二行一旦返回对齐状态,则为*".我将如何添加具有这些特征的定义?

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?

推荐答案

尝试:

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

或者,您可以有两个网格,并且只将可见性修改为视觉状态的一部分,这样您就不会在代码中进行大量繁琐的 UI 操作.内置的 Visual Studio 模板将这种技术用于捕捉视图.

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.

这篇关于在 C# 中删除和添加 GridRow 定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆