[英]Grid doesn't stretch inside StackPanel
我有两个高度恒定的文本框,我想垂直堆叠这些文本框和一个网格。 我尝试过使用stackpanel,但是网格没有拉伸,并且始终保持相同大小(尽可能最小)。
<StackPanel Orientation="Vertical" MaxWidth="110">
<TextBox Background="White" Height="40" Text="some text1"/>
<TextBox Background="White" Height="40" Text="some text2"/>
<Grid x:Name="internalGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" MaxWidth="110"/>
<ColumnDefinition Width="1*" MaxWidth="110"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" MaxHeight="300"/>
<RowDefinition Height="1*" MaxHeight="300"/>
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0"/>
<Button Grid.Column="0" Grid.Row="1"/>
<Button Grid.Column="1" Grid.Row="0"/>
<Button Grid.Column="1" Grid.Row="1"/>
</Grid>
</StackPanel>
我也尝试使用Grid而不是Stackpanel,但是当我以全屏模式制作应用程序时,文本框和内部网格之间会有一个空白
StackPanel
行为不像这样。 它占用的空间最小。 改用Grid并定义RowDefinition
。 默认情况下,网格内的空间在行之间平均分配(高度设置为“ *”),因此您必须将高度设置为“自动”,以使行占据的最小空间:
<Grid MaxWidth="110">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Background="White"
Height="40"
Text="some text1" />
<TextBox Grid.Row="1" Background="White"
Height="40"
Text="some text2" />
<Grid Grid.Row="2" x:Name="internalGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"
MaxWidth="110" />
<ColumnDefinition Width="1*"
MaxWidth="110" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"
MaxHeight="300" />
<RowDefinition Height="1*"
MaxHeight="300" />
</Grid.RowDefinitions>
<Button Grid.Column="0"
Grid.Row="0" />
<Button Grid.Column="0"
Grid.Row="1" />
<Button Grid.Column="1"
Grid.Row="0" />
<Button Grid.Column="1"
Grid.Row="1" />
</Grid>
</Grid>
或者尝试使用DockPanel
:
<DockPanel MaxWidth="110" LastChildFill="True" >
<TextBox DockPanel.Dock="Top" Background="White"
Height="40"
Text="some text1" />
<TextBox DockPanel.Dock="Top" Background="White"
Height="40"
Text="some text2" />
<Grid DockPanel.Dock="Top" x:Name="internalGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"
MaxWidth="110" />
<ColumnDefinition Width="1*"
MaxWidth="110" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"
MaxHeight="300" />
<RowDefinition Height="1*"
MaxHeight="300" />
</Grid.RowDefinitions>
<Button Grid.Column="0"
Grid.Row="0" />
<Button Grid.Column="0"
Grid.Row="1" />
<Button Grid.Column="1"
Grid.Row="0" />
<Button Grid.Column="1"
Grid.Row="1" />
</Grid>
</DockPanel>
您可以使用IMultiValueConverter
实现此IMultiValueConverter
:
[ValueConversion(typeof(double), typeof(double))]
public class MyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var total = (double)values.FirstOrDefault();
var subtract = values.Cast<double>().Sum();
return total + total - subtract;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
您可以在资源部分中的XAML中介绍您的转换器:
<Window.Resources>
<local:MyConverter x:Key="Converter"></local:MyConverter>
</Window.Resources>
并且您的XAML将更改为:
<StackPanel Orientation="Vertical" MaxWidth="110" Background="Red">
<TextBox Name="Label1" Background="White" Height="40" Text="some text1"/>
<TextBox Name="Label2" Background="White" Height="40" Text="some text2"/>
<Grid x:Name="internalGrid" Background="Yellow" >
<Grid.Height>
<MultiBinding Converter="{StaticResource Converter}">
<Binding RelativeSource="{RelativeSource AncestorType=StackPanel}" Path="ActualHeight"/>
<Binding ElementName="Label1" Path="ActualHeight"/>
<Binding ElementName="Label2" Path="ActualHeight"/>
</MultiBinding>
</Grid.Height>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" MaxWidth="110"/>
<ColumnDefinition Width="1*" MaxWidth="110"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" MaxHeight="300"/>
<RowDefinition Height="1*" MaxHeight="300"/>
</Grid.RowDefinitions>
</Grid>
</StackPanel>
当然,背景颜色只是为了更加清楚,而不是必须的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.