尝试将窗口的宽度和高度设置为自动

时间:2012-08-02 00:10:25

标签: wpf xaml height width

我正在尝试将窗口的宽度和高度设置为auto,这意味着它将具有所有子控件的宽度和高度组合:

我的代码:

<Window x:Class="Read_360.ToastPopup"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ToastPopup" Width="Auto" Height="68">
<Window.Resources>
    <ControlTemplate x:Key="MyErrorMessage"
                     TargetType="{x:Type Label}">
        <StackPanel Orientation="Horizontal"
                    Name="ErrorMessage" Width="Auto" Height="Auto">
            <Border CornerRadius="4" Width="Auto"
                    BorderThickness="0"
                    VerticalAlignment="Center"
                    Margin="4,0"
                    Background="#FF404040"
                    BorderBrush="Transparent">
                <Label Foreground="White"
                       VerticalAlignment="Center"
                       Padding="4,2"
                       Margin="4"
                       FontWeight="Bold" FontSize="15"
                       Name="Part1" Width="Auto" Height="Auto"
                       Visibility="Visible" HorizontalAlignment="Center" Content="{Binding Message}" />
            </Border>
        </StackPanel>
    </ControlTemplate>
</Window.Resources>
<StackPanel Orientation="Vertical">
    <Label Template="{StaticResource MyErrorMessage}" Name="Label1" Width="Auto" Height="Auto"></Label>
</StackPanel>

我需要宽度和高度与ControlTemplate内的Label相同

先谢谢了,

锭。

2 个答案:

答案 0 :(得分:6)

您应该可以将窗口上的SizeToContent属性设置为WidthAndHeight来实现此目的。

答案 1 :(得分:1)

尝试设置属性

SizeToContent="WidthAndHeight"

但为什么高度未设置为自动?