我有一个用户控件,它只是一个包含如下文本的框:
<Border x:Name="box"
BorderThickness="0"
BorderBrush="Black"
Background="Black"
Width="200" Height="200">
<Label Content="Hello World"
Height="65" Width="400"
Foreground="White" FontSize="32"
HorizontalAlignment="Center" VerticalAlignment="Center"
HorizontalContentAlignment="Center" />
</Border>
当我在我的MainWindow中创建和实例时,就像这样...
<local:MyBoxControl Width="1000" Height="1000"/>
Border
中定义的宽度和高度为200x200,我希望它在创建时提供1000x1000。
答案 0 :(得分:4)
只需从Border元素中删除Width和Height。
答案 1 :(得分:1)
您可以将Border的HorizontalAlignment和VerticalAlignment设置为Stretch吗?
答案 2 :(得分:1)
假设您需要将边框的宽度/高度保持为200(否则您只是不指定它),您可以将网格设置为控件的最外层元素并将边框放在其中。然后,如果你没有定义宽度/高度,它将自动调整到你的控件的大小。
<Grid>
<Border x:Name="box"
BorderThickness="0"
BorderBrush="Black"
Background="Black"
Width="200" Height="200">
<Label Content="Hello World"
Height="65" Width="400"
Foreground="White" FontSize="32"
HorizontalAlignment="Center" VerticalAlignment="Center"
HorizontalContentAlignment="Center" />
</Border>
</Grid>