根据内容大小设置网格高度

时间:2016-02-22 12:39:25

标签: c# wpf grid height

我有单独的Window。它包含Grid。我需要根据其中的内容设置此Height的{​​{1}}。

一般来说,此GridWindow类似,因此我想在其中显示所有内容。

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> 的{​​{1}}很大时 - 我想让窗口也更大,以显示其中的所有文字。但是为Text设置TextBlockHeight没有帮助,导致Grid的高度在所有可能的空间上,例如窗口的高度。

Auto设置为基础Grid也无济于事,因为SizeToContent="Height"应该在所有屏幕上,并且此属性不会执行此操作。

1 个答案:

答案 0 :(得分:3)

将网格的VerticalAlignment设置为Center