为什么 WPF 网格的行高不调整为其内容的高度?



我有一个类似的网格:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <TextBlock Grid.Row="0" Grid.Column="0" Text="MyHeader1"/>
    <myNamespace:MyRotatedTextBlock
        Grid.Row="1" Grid.Column="0" MyText="MyHeader2"/>
</Grid>

myNamespace:MyRotatedTextBlock是一个自定义的WPF控件,如下所示:

<TextBlock Text="{Binding MyText}"
    HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock.LayoutTransform>
         <RotateTransform Angle="90"/>
    </TextBlock.LayoutTransform>
</TextBlock>

问题是,当我打开窗口时,我看不到包含旋转文本的第二行。但是,如果我用"100"替换第二行的Height(设置为"Auto"),那么我可以看到第二行显示出来,并且它包含MyHeader2

您也可以从TextBlock(而不是userControl)派生,如下所示:

<TextBlock x:Class="WpfGridRowHeightStackOverflowQuestion.MyRotatedTextBlock"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             HorizontalAlignment="Center"
             VerticalAlignment="Center">
    <TextBlock.LayoutTransform>
         <RotateTransform Angle="90"/>
    </TextBlock.LayoutTransform>
</TextBlock>

然后只需使用TextBlock中的Text属性,如下所示:

<myNamespace:MyRotatedTextBlock Grid.Row="1" Grid.Column="0" Text="MyHeader2"></myNamespace:MyRotatedTextBlock>

编辑

通过这种方式,它也可以作为UserControl工作(因为绑定的elementname被明确指定为用户控件的名称):

<UserControl x:Class="WpfGridRowHeightStackOverflowQuestion.MyRotatedTextBlock"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
                         Name="CustomRotatedTextBlock">
    <TextBlock Text="{Binding ElementName=CustomRotatedTextBlock,Path=MyText}" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBlock.LayoutTransform>
         <RotateTransform Angle="90"/>
    </TextBlock.LayoutTransform>
    </TextBlock>
</UserControl>

然后,我在代码后面使用INotifyPropertyChanged的Change Notification(WPF非常依赖它;)

public partial class MyRotatedTextBlock : UserControl, INotifyPropertyChanged
{
    public MyRotatedTextBlock()
    {
        InitializeComponent();
    }
    private String _myText;
    public String MyText
    {
        get { return _myText; }
        set { 
            _myText = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("MyText"));
        }
    }
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
}

您尝试过UpdateLayout吗?打开窗口后尝试网格的UpdateLayout

最新更新