C# 动态行定义高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1207750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:56:13  来源:igfitidea点击:

Dynamic RowDefinition Height

c#wpfgrid

提问by James

I have a simple xaml control with the following Grid Row definition:

我有一个带有以下网格行定义的简单 xaml 控件:

<Grid.RowDefinitions>
            <RowDefinition Height="15*" />
            <RowDefinition Height="60*" />
            <RowDefinition Height="20*" />
            <RowDefinition Height="20*" />
            <RowDefinition Height="15*" />
</Grid.RowDefinitions>

Rows 1-3 each hold a text block which may or may not have text in it. In the code behind I want to minimise the RowDefinition if there is no text. Essentially I have the following in my code behind:

第 1-3 行各包含一个文本块,其中可能包含或不包含文本。在后面的代码中,如果没有文本,我想最小化 RowDefinition。基本上我的代码中有以下内容:

if(textblock.Text != ""){
   grid.RowDefinitions[elementRow].Height = new GridLength(20, GridUnitType.Star);
}
else{
   grid.RowDefinitions[elementRow].Height = new GridLength(0, GridUnitType.Star);
}

I want rows 0 and 4 to stay as they are defined in the xaml. Unfortunatly this does not work even though there is text in the text block on row 2 nothing is displayed.

我希望第 0 行和第 4 行保持它们在 xaml 中的定义。不幸的是,即使第 2 行的文本块中有文本,这也不起作用,但没有显示任何内容。

Am I doing something wrong.

难道我做错了什么。

Any help is appreciated,

任何帮助表示赞赏,

James

詹姆士

采纳答案by Charlie

Don't use the star notation, use Auto for your RowDefinitions. If the TextBlock.Text is empty, set the Visibility of the TextBlock to Visibility.Collapsed. The grid row will then automatically shrink to nothing.

不要使用星号,为您的 RowDefinitions 使用 Auto。如果 TextBlock.Text 为空,则将 TextBlock 的可见性设置为 Visibility.Collapsed。然后网格行将自动缩小为空。

回答by Jobi Joy

You can put your items inside a UniformGridwith Columns="1" And make the TextBox Visibility to collapsed when you get emptry text.

您可以将项目放在Columns="1"的UniformGrid 中,并在获得空文本时使 TextBox Visibility 折叠。

 <UniformGrid Columns="1">
    <TextBlock Text="AAAA" Visibility="Collapsed" Grid.Row="0"/>
    <TextBlock Text="BBBBB" Grid.Row="1"/>
    <TextBlock Text="CCCCC" Grid.Row="2"/>
    <TextBlock Text="DDDDD" Grid.Row="3"/>
    <TextBlock Text="EEEE" Grid.Row="4"/>
</UniformGrid>

回答by Carlo

This is not the answer to your question, just some info.

这不是您问题的答案,只是一些信息。

The * in the Height (or width for columns) means that the row (or column) width Height="*" (or Width="*") will take up the rest of the space. So if you have a grid with 4 rows in a grid with Height="100", if you do this:

高度(或列宽)中的 * 表示行(或列)宽度 Height="*"(或 Width="*")将占据其余空间。因此,如果您的网格中有 4 行且 Height="100" 的网格,请执行以下操作:

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

The row width Height="*" will be 70 DIUs (device independent units).

行宽 Height="*" 将为 70 DIU(设备独立单位)。

Adding a number before the asterisk (Height="2*") only works if there are more than one rows using the asterisk, the number before the asterisk indicates how much more space will that specific row take (2* = twice as much, 3* three times as much, so on...). I. E.:

在星号前添加一个数字 (Height="2*") 仅当使用星号的行超过一行时才有效,星号前的数字表示该特定行占用多少空间(2* = 两倍, 3* 三倍,依此类推...)。IE:

<Grid.RowDefinitions>
            <RowDefinition Height="10" />
            <RowDefinition Height="10" />
            <RowDefinition Height="2*" /> <!-- this row will be twice as tall as the one below -->
            <RowDefinition Height="*" />
</Grid.RowDefinitions>

Here the 3rd row will have a height of 54 DIUs (twice as much as the 4th row which has a height of 26 DIUs approx.), both heights sum 80, which is the rest of the space of the grid (10 + 10 + 26 + 54 = 100, the grid height).

这里第 3 行的高度为 54 DIU(大约是第 4 行的两倍,高度为 26 DIU),两个高度总和为 80,这是网格的其余空间 (10 + 10 + 26 + 54 = 100,网格高度)。

BTW, I agree with Charlie's answer.

顺便说一句,我同意查理的回答。