【发布时间】:2016-06-03 21:33:09
【问题描述】:
我有单独的Window。它包含Grid。我需要根据里面的内容设置这个Grid的Height。
一般来说,这个Window 就像Popup 所以我想显示其中的所有内容。
<Window x:Class="Stream.WPF.Popups.CustomMessageBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:strings="clr-namespace:Stream.WPF.Strings"
Background="#7F000000" AllowsTransparency="True"
WindowState="Maximized" WindowStyle="None" ResizeMode="NoResize"
WindowStartupLocation="CenterScreen">
<Grid Width="450" Background="#61596f">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="75" />
</Grid.RowDefinitions>
<TextBlock Grid.ColumnSpan="2" VerticalAlignment="Center" Foreground="#FFC0B6D1" TextAlignment="Center" FontFamily="Segoe UI Semibold" FontSize="18" >
<TextBlock Name="MessageTextBlock" Text="Unable to connect to the sensor. The program will now close.Unable to connect to the sensor. The program will now close.Unable to connect to the sensor. The program will now close.Unable to connect to the sensor. The program will now close.Unable to connect to the sensor. The program will now close." TextWrapping="Wrap" Margin="10 0"/>
</TextBlock>
<Button Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Name="CloseBtn" Background="Transparent" BorderBrush="Transparent" Content="{x:Static strings:Resource.CloseBtn}" PreviewMouseUp="Cancel_Click" Foreground="#fff" FontFamily="Segoe UI Semibold" FontSize="14">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="bdr_main" Background="Transparent" Height="44" VerticalAlignment="Top" BorderBrush="#c0b6d1" BorderThickness="2" CornerRadius="2" Margin="30,0,15,0">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Margin="8,6,8,6" ContentSource="Content" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bdr_main" Property="Background" Value="Transparent"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="bdr_main" Property="Background" Value="#7FC0B6D1"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>
当TextBlock 的Text 很大时 - 我想让窗口也更大以显示里面的所有文本。但是将Height 设置为Auto 为Grid 并没有帮助,因为网格的高度在所有可能的空间上,比如窗口的高度。
将SizeToContent="Height" 设置为基础Window 也无济于事,因为Window 应该在所有屏幕上,而此属性不会这样做。
【问题讨论】:
-
有
Grid的静态Width和TextBlock的静态字体大小 - 问题是在哪里 / 何时你改变大小TextBlock的? -
尝试使用“*”而不是“Auto”,并在 Textblock 属性处给出高度参数
-
@Jasper,好吧,我设置了
Width,因为我希望它是静态的。对于网格,我没有设置高度,因为我希望它对内容是动态的(尤其是关于第一行,我还设置了高度,如自动)。因为 TextBlock 有属性TextWrapping="Wrap"它会在可用空间内显示文本