试图窗口的宽度和高度设置为自动
问题描述:
我试图使窗口的宽度和高度是自动,这意味着将有它的所有儿童的宽度和高度控制相结合:试图窗口的宽度和高度设置为自动
我代码:
<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>
我所需要的宽度和高度是相同的这是在先进的控件模板
由于内部的标签,
Din。
答
尝试设置属性
SizeToContent="WidthAndHeight"
但为什么高度没有被设置为自动?
谢谢,但现在我需要得到窗口的宽度和高度后,它的大小,宽度属性和ActualWidth属性显示数字0,我需要它,以便计算我的窗口的位置和把它放在屏幕的中间。再次感谢。 – dinbrca 2012-08-02 09:32:54
窗口上的WindowStartupLocation =“CenterScreen”应该这样做;) – 2012-08-02 22:47:13
再次感谢:) – dinbrca 2012-08-03 00:16:41