在代码中指定 RowDefinition.Height

发布于 2024-12-02 15:42:46 字数 594 浏览 1 评论 0原文

当您在 xaml 中创建 Grid 时,您可以定义 RowDefinitions,因此

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

我需要在代码中执行相同的操作。我知道我可以写,

RowDefinition row = new RowDefinition();
row.Height = new GridLength(1.0, GridUnitType.Star);

但这对我没有多大帮助,因为我收到了一个字符串。我可能可以创建自己的“字符串到 GridLength”转换器,但这感觉不太对,因为它从 xaml 工作得如此顺利。当然,我已经尝试过以下方法,但它不起作用

row.Height = new GridLength("*");

我在这里缺少什么?

When you're creating a Grid in xaml you can define the RowDefinitions as such

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

I have a need to do the same thing in code. I know I can write

RowDefinition row = new RowDefinition();
row.Height = new GridLength(1.0, GridUnitType.Star);

but that doesn't help me much since I've got a string coming in. I could probably create my own "string to GridLength" converter but this doesn't feel right since it works ever so smooth from xaml. Of course, I've tried the following but it doesn't work

row.Height = new GridLength("*");

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁吢 2024-12-09 15:42:46

GridLength 结构定义了一个 TypeConverter,从 Xaml 实例化时将使用该类型。您也可以在代码中使用它。它的名称为 GridLengthConverter

如果您使用 Reflector 查看 GridLength.cs,它看起来像这样。注意 TypeConverter

[StructLayout(LayoutKind.Sequential), TypeConverter(typeof(GridLengthConverter))]
public struct GridLength : IEquatable<GridLength>
{
    //...
}

您可以像这样使用它

GridLengthConverter gridLengthConverter = new GridLengthConverter();
row.Height = (GridLength)gridLengthConverter.ConvertFrom("*");

The GridLength struct has a TypeConverter defined which is being used when instantiated from Xaml. You can use it in code as well. It's called GridLengthConverter

If you look at GridLength.cs with Reflector it looks like this. Notice the TypeConverter

[StructLayout(LayoutKind.Sequential), TypeConverter(typeof(GridLengthConverter))]
public struct GridLength : IEquatable<GridLength>
{
    //...
}

You can use it like

GridLengthConverter gridLengthConverter = new GridLengthConverter();
row.Height = (GridLength)gridLengthConverter.ConvertFrom("*");
肤浅与狂妄 2024-12-09 15:42:46

您缺少将 RowDefinition 包含到 RowDefinitions 中,

RowDefinition row = new RowDefinition();
row.Height = new GridLength(1.0, GridUnitType.Star);
YourGrid.RowDefinitions.Add(row);

再见!
芸香科

you are missing to include your RowDefinition to RowDefinitions

RowDefinition row = new RowDefinition();
row.Height = new GridLength(1.0, GridUnitType.Star);
YourGrid.RowDefinitions.Add(row);

See you!
Rutx

枕梦 2024-12-09 15:42:46

无需创建转换器,已经有一个转换器,它也被 XAML 解析器使用:

var converter = new GridLengthConverter();
row.Height = (GridLength)converter.ConvertFromString("*");

顺便说一句,您会发现像这样的转换器适用于很多类型,因为许多类型都可以使用这样的转换器。从 XAML 中的字符串解析,例如 BrushConverter & 图像源转换器

No need to create a converter, there already is one, which is being used by the XAML-parser as well:

var converter = new GridLengthConverter();
row.Height = (GridLength)converter.ConvertFromString("*");

On a sidenote, you will find converters like this for a lot of types, as many get parsed from strings in XAML, e.g. BrushConverter & ImageSourceConverter

无敌元气妹 2024-12-09 15:42:46

按照 Rutx 的回答,我会把它写得更短

grdLbx.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });

Following Rutx answer I would make it even shorter

grdLbx.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文