【问题标题】:Set Height of Grid according to content size根据内容大小设置网格高度
【发布时间】:2016-06-03 21:33:09
【问题描述】:

我有单独的Window。它包含Grid。我需要根据里面的内容设置这个GridHeight

一般来说,这个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>

TextBlockText 很大时 - 我想让窗口也更大以显示里面的所有文本。但是将Height 设置为AutoGrid 并没有帮助,因为网格的高度在所有可能的空间上,比如窗口的高度。

SizeToContent="Height" 设置为基础Window 也无济于事,因为Window 应该在所有屏幕上,而此属性不会这样做。

【问题讨论】:

  • Grid 的静态WidthTextBlock 的静态字体大小 - 问题是在哪里 / 何时你改变大小TextBlock 的?
  • 尝试使用“*”而不是“Auto”,并在 Textblock 属性处给出高度参数
  • @Jasper,好吧,我设置了Width,因为我希望它是静态的。对于网格,我没有设置高度,因为我希望它对内容是动态的(尤其是关于第一行,我还设置了高度,如自动)。因为 TextBlock 有属性 TextWrapping="Wrap" 它会在可用空间内显示文本

标签: c# wpf grid height


【解决方案1】:

将网格的VerticalAlignment 设置为Center

【讨论】:

    猜你喜欢
    • 2016-03-13
    • 1970-01-01
    • 2022-09-27
    • 2020-04-02
    • 1970-01-01
    • 2018-10-29
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多