首页 文章

仅按内容填充容器

提问于
浏览
1

我正面临布局问题 . 我需要创建一个窗口,其布局适用于以下两种情况:

enter image description here

我尝试使用DockPanel,但这样黄色容器即使没有内容也会拉伸 . 我需要对内容(UserControl)做一些事情来实现它 .

EDIT: "Image's "内容固定" = "内容固定高度“

2 回答

  • 0

    我想你可以使用 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>
    

    和转换器本身:

    public class BooleanToVerticalAlignment : IValueConverter
    {
        public VerticalAlignment FalseAlignment { get; set; }
    
        public VerticalAlignment TrueAlignment { get; set; }
    
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (bool)value ? TrueAlignment : FalseAlignment;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
  • 0

    使用自定义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:

    <UserControl.Resources>
        <stackOverflow:BoolToGridLengthConverter x:Key="BoolToGridSizeConverter" TrueLength="1*" FalseLength="Auto"/>
        <stackOverflow:BoolToGridLengthConverter x:Key="BoolToGridSizeConverter2" TrueLength="0" FalseLength="1*"/>
    </UserControl.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="{Binding ElementName=ChangingSizeControl, Path=HasContent, Converter={StaticResource BoolToGridSizeConverter}}"/>
            <RowDefinition Height="{Binding ElementName=ChangingSizeControl, Path=HasContent, Converter={StaticResource BoolToGridSizeConverter2}}"/>
        </Grid.RowDefinitions>
        <UserControl Grid.Row="0" x:Name="FixedSizeControl"/>
        <ContentControl Grid.Row="1" x:Name="ChangingSizeControl"/>
        <UserControl Grid.Row="2" x:Name="MainContentControl"/>
    </Grid>
    

相关问题