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

时间:2012-01-23 00:42:26

标签: wpf grid layouttransform

我有一个类似于此的网格:

<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>

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

2 个答案:

答案 0 :(得分:1)

你也可以从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(因为绑定的元素名明确指定给用户控件的名称):

<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
}

答案 1 :(得分:0)

你试过UpdateLayout吗?打开窗口后尝试UpdateLayout为网格