Wpf 使容器仅按内容填充

Wpf 使容器仅按内容填充,wpf,wpf-controls,Wpf,Wpf Controls,我面临一个布局问题。我需要制作一个布局适用于以下两种情况的窗口: 我试着用DockPanel做了些什么,但这样黄色的容器即使没有内容也会被拉伸。我需要对内容(一个用户控件)做一些事情来实现它 编辑:使用“=”内容固定高度“使用自定义的IValueConverter根据中间控件是否有任何内容切换行上的高度值 自定义IValueConverter: public class BoolToGridLengthConverter : IValueConverter { public GridL

我面临一个布局问题。我需要制作一个布局适用于以下两种情况的窗口:

我试着用DockPanel做了些什么,但这样黄色的容器即使没有内容也会被拉伸。我需要对内容(一个用户控件)做一些事情来实现它


编辑:使用“=”内容固定高度“

使用自定义的IValueConverter根据中间控件是否有任何内容切换行上的高度值

自定义IValueConverter:

public class BoolToGridLengthConverter : IValueConverter
{
    public GridLength TrueLength { get; set; }

    public GridLength FalseLength { get; set; } 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)value ? TrueLength : FalseLength;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
XAML:


使用自定义IValueConverter根据中间控件是否包含任何内容来切换行上的高度值

自定义IValueConverter:

public class BoolToGridLengthConverter : IValueConverter
{
    public GridLength TrueLength { get; set; }

    public GridLength FalseLength { get; set; } 

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)value ? TrueLength : FalseLength;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
XAML:


我想您可以使用
IValueConverter
来实现此功能

<Grid>
    <Grid.Resources>
        <converters:NullableToVerticalAlignment FalseAlignment="Top" 
                                                TrueAlignment="Stretch" />
    </Grid.Resources>

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

    <custom:ContentWithFixedHeight />

    <custom:ContentWithVariableHeight Grid.Row="1"
                                      VerticalAlignment="{Binding Content,
                                                                  RelativeSource={RelativeSource Mode=Self},
                                      Converter={StaticResource NullableToVerticalAlignmentConverter}}" />
</Grid>

我想你可以用
IValueConverter
来做这个

<Grid>
    <Grid.Resources>
        <converters:NullableToVerticalAlignment FalseAlignment="Top" 
                                                TrueAlignment="Stretch" />
    </Grid.Resources>

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

    <custom:ContentWithFixedHeight />

    <custom:ContentWithVariableHeight Grid.Row="1"
                                      VerticalAlignment="{Binding Content,
                                                                  RelativeSource={RelativeSource Mode=Self},
                                      Converter={StaticResource NullableToVerticalAlignmentConverter}}" />
</Grid>

他可能因为我们不知道的原因希望视图显示(但为空)。他可能因为我们不知道的原因希望视图显示(但为空)。