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

C# 根据内容大小设置网格高度,c#,wpf,grid,height,C#,Wpf,Grid,Height,我有单独的窗口。它包含网格。我需要根据网格中的内容设置该网格的高度 通常,此窗口类似于弹出窗口,因此我希望显示其中的所有内容 <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"

我有单独的
窗口
。它包含
网格
。我需要根据网格中的内容设置该网格的高度

通常,此
窗口类似于
弹出窗口
,因此我希望显示其中的所有内容

<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
没有帮助,因为Grid的高度在所有可能的空间上,比如窗口的高度


SizeToContent=“Height”
设置为基本
窗口也没有帮助,因为
窗口应在所有屏幕上,而此属性不起作用。

将网格的
垂直对齐设置为
中心

将网格的
垂直对齐设置为
中心

网格的
宽度
文本块的静态字体大小
问题是在何处/何时更改
TextBlock
?尝试使用“*”而不是“Auto”,并在TextBlock处给出高度参数Attribute@Jasper我设置了宽度,因为我希望它是静态的。对于网格,我并没有设置高度,因为我希望它对于内容是动态的(特别是关于第一行,我也像自动一样设置高度)。原因TextBlock具有属性
TextWrapping=“Wrap”
它将在可用空间内显示文本有一个静态
宽度
网格和静态字体大小
TextBlock
-问题是在何处/何时更改
TextBlock
的大小?尝试使用“*”而不是“自动”并在Textblock处给出高度参数Attribute@Jasper我设置了宽度,因为我希望它是静态的。对于网格,我并没有设置高度,因为我希望它对于内容是动态的(特别是关于第一行,我也像自动一样设置高度)。原因TextBlock具有属性
TextWrapping=“Wrap”
它将在可用空间内显示文本