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

din*_*rca 1 wpf height xaml 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>
Run Code Online (Sandbox Code Playgroud)

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

提前致谢,

声浪.

小智 6

您应该能够将窗口上的SizeToContent属性设置为WidthAndHeight来实现此目的.