【问题标题】:Trying to set width and height of the window to auto尝试将窗口的宽度和高度设置为自动
【发布时间】:2012-07-30 22:39:51
【问题描述】:

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

我的代码:

<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 内的标签相同

非常感谢,

丁。

【问题讨论】:

    标签: wpf xaml height width


    【解决方案1】:

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

    【讨论】:

    • 谢谢,但现在我需要在调整大小后获取窗口的宽度和高度,并且 Width 属性和 ActualWidth 属性显示数字 0,我需要它来计算我的窗口的位置并将其放在屏幕的中间。再次感谢。
    • WindowStartupLocation="CenterScreen" on Window 应该这样做;)
    【解决方案2】:

    尝试设置属性

    SizeToContent="WidthAndHeight"
    

    但是为什么高度没有设置为自动呢?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 2014-03-05
      • 2015-02-26
      • 1970-01-01
      • 2012-09-02
      相关资源
      最近更新 更多