动态添加的UserControl不会调整为父级大小

时间:2022-06-22 23:39:55

I have a UserControl which will be loaded at runtime into a ContentControl. The user control does not adjust its size to the parent control even I haven't set Width nor Height. What is it that I miss?

我有一个UserControl,它将在运行时加载到ContentControl中。即使我没有设置Width也不设置Wight,用户控件也不会将其大小调整为父控件。我错过了什么?

        <ContentControl x:Name="MainContentControl" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" >
        <Label  Background="LightGray" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" >Sample control </Label>
    </ContentControl>

The sample control ( label ) inside the ContentControl works just fine and expands depending on size changes of the ContentControl.

ContentControl中的样本控件(标签)工作正常,并根据ContentControl的大小更改进行扩展。

When I now replace the Label with an UserControl the size of the Usercontrol has as maxsize the position of the label which is inside it

当我现在用UserControl替换Label时,Usercontrol的大小为maxsize其内部标签的位置

Here the UserControl

这里是UserControl

<UserControl x:Class="test.testUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Label Content="Label" HorizontalAlignment="Left" Margin="235,200,0,0" VerticalAlignment="Top"/>
</Grid>

1 个解决方案

#1


0  

You are using HorizontalAlignment and VerticalAlignment which tells WPF "draw this control with only the size it needs, then align it in the parent container according to these rules".

您正在使用Horizo​​ntalAlignment和VerticalAlignment,它告诉WPF“仅使用它需要的大小绘制此控件,然后根据这些规则将其对齐在父容器中”。

So your label is getting drawn to the exact size it needs, then placed in the Top Left corner.

因此,您的标签将被绘制到所需的确切尺寸,然后放置在左上角。

If your top section of code, you are using HorizontalContentAlignment and VerticalContentAlignement, which means "align the content inside this control like this".

如果是代码的顶部,则使用Horizo​​ntalContentAlignment和VerticalContentAlignement,这意味着“将此控件内的内容对齐”。

So HorizontalContentAlignment="Center" VerticalContentAlignment="Center" would draw the Label according to whatever the parent container's rules are (sounds like in your case Stretch), then the content inside the Label would get positioned in the middle.

所以Horizo​​ntalContentAlignment =“Center”VerticalContentAlignment =“Center”会根据父容器的规则绘制Label(听起来像你的Stretch),然后Label内的内容将位于中间。

So either change your Horizonta/VerticalAlignment properties to Horizontal/VerticalContentAlignment, or set the value of them Stretch

因此,要么将Horizo​​nta / VerticalAlignment属性更改为Horizo​​ntal / VerticalContentAlignment,要么设置它们的值Stretch

#1


0  

You are using HorizontalAlignment and VerticalAlignment which tells WPF "draw this control with only the size it needs, then align it in the parent container according to these rules".

您正在使用Horizo​​ntalAlignment和VerticalAlignment,它告诉WPF“仅使用它需要的大小绘制此控件,然后根据这些规则将其对齐在父容器中”。

So your label is getting drawn to the exact size it needs, then placed in the Top Left corner.

因此,您的标签将被绘制到所需的确切尺寸,然后放置在左上角。

If your top section of code, you are using HorizontalContentAlignment and VerticalContentAlignement, which means "align the content inside this control like this".

如果是代码的顶部,则使用Horizo​​ntalContentAlignment和VerticalContentAlignement,这意味着“将此控件内的内容对齐”。

So HorizontalContentAlignment="Center" VerticalContentAlignment="Center" would draw the Label according to whatever the parent container's rules are (sounds like in your case Stretch), then the content inside the Label would get positioned in the middle.

所以Horizo​​ntalContentAlignment =“Center”VerticalContentAlignment =“Center”会根据父容器的规则绘制Label(听起来像你的Stretch),然后Label内的内容将位于中间。

So either change your Horizonta/VerticalAlignment properties to Horizontal/VerticalContentAlignment, or set the value of them Stretch

因此,要么将Horizo​​nta / VerticalAlignment属性更改为Horizo​​ntal / VerticalContentAlignment,要么设置它们的值Stretch