C# 动态行定义高度

C# 动态行定义高度,c#,wpf,grid,C#,Wpf,Grid,我有一个简单的xaml控件,具有以下网格行定义: <Grid.RowDefinitions> <RowDefinition Height="15*" /> <RowDefinition Height="60*" /> <RowDefinition Height="20*" /> <RowDefinition Height="20*" />

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

<Grid.RowDefinitions>
            <RowDefinition Height="15*" />
            <RowDefinition Height="60*" />
            <RowDefinition Height="20*" />
            <RowDefinition Height="20*" />
            <RowDefinition Height="15*" />
</Grid.RowDefinitions>
我希望第0行和第4行保持在xaml中定义的状态。不幸的是,即使第2行的文本块中有文本,也无法显示任何内容

我做错什么了吗

感谢您的帮助


James

不要使用星号符号,使用Auto来定义行。如果TextBlock.Text为空,请将TextBlock的可见性设置为Visibility.Collapsed。然后,网格行将自动收缩为零。

您可以将项目放入带有Columns=“1”的UniformGrid中,并在获取空文本时使文本框可见并折叠

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

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

高度(或列宽度)中的*表示行(或列)宽度高度=“*”(或宽度=“*”)将占用剩余空间。因此,如果在高度为100的网格中有一个包含4行的网格,如果这样做:

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

行宽高度=“*”将为70 DIU(设备独立单元)

只有当有多行使用星号时,在星号前添加一个数字(Height=“2*”)才有效,星号前的数字表示该特定行将占用多少空间(2*=两倍,3*三倍,依此类推……)。即:


此处,第三排的高度为54 DIU(约为第四排26 DIU高度的两倍),两排高度之和为80,即网格剩余空间(10+10+26+54=100,网格高度)

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

<Grid.RowDefinitions>
            <RowDefinition Height="10" />
            <RowDefinition Height="10" />
            <RowDefinition Height="10" />
            <RowDefinition Height="*" />
</Grid.RowDefinitions>
<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>