自定义控件不会随着窗口大小的调整而调整

时间:2021-01-14 13:03:22

标签: c# wpf xaml responsiveness

我有一个具有这种布局的自定义控件:

<Border x:Name="PART_OuterBorder"
    CornerRadius="1000"
    BorderThickness="3"
    BorderBrush="Black">
    <Border.Background>
        <RadialGradientBrush>
            <RadialGradientBrush.GradientStops>
                <GradientStop Offset="0" Color="#438F40"/>
                <GradientStop Offset="0.95" Color="#0D441D"/>
                <GradientStop Offset="2" Color="#106E42"/>
            </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
    </Border.Background>

    <Border x:Name="PART_InnerBorder"
        CornerRadius="1000">
        <Border.Background>
            <RadialGradientBrush>
                <RadialGradientBrush.GradientStops>
                    <GradientStop Offset="0" Color="#333439"/>
                    <GradientStop Offset="1.5" Color="#000"/>
                </RadialGradientBrush.GradientStops>
            </RadialGradientBrush>
        </Border.Background>
        <userControls:DraggableElement x:Name="PART_DraggableElement"
                                Width="16" Height="16"/>
    </Border>
</Border>

如您所见,我没有为 2 个边框设置宽度和高度,而是在网格行中使用了控件:

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

    <customControls:myCustomControl Grid.Row="1" HorizontalAlignment="Stretch"
                                      VerticalAlignment="Stretch"/>
</Grid>

但是当我调整窗口大小时,控件不会调整大小。顺便说一句,如果我把它放在一个更小或更大的网格单元中,它会固定它的大小,这是它唯一一次固定它的大小。

我还考虑使用 ViewBox 作为自定义控件模板中边框的父级,但这会改变整个布局:

正常:

enter image description here

带有视图框:

enter image description here

0 个答案:

没有答案