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”
它将在可用空间内显示文本