【问题标题】:Why doesn't the Row Height of a WPF Grid resize to the Height of its content?为什么 WPF 网格的行高不调整为其内容的高度?
【发布时间】:2012-01-23 00:42:25
【问题描述】:

我有一个类似这样的网格:

<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 应自动调整其内容的大小。我可以看到您已将控件的 Text 属性绑定到“MyText”,并且您将该属性设置为“MyHeader2” - 您的自定义控件中的 Text 绑定是否有问题?
  • (我编辑了问题。)不正确绑定,因为当我手动扩展行高时显示。
  • 为什么你实际上引入了一个新属性 MyText 而没有使用你(可能)派生自的 TextBlock 的 Text 属性?
  • 我真傻!谢谢你。这解决了问题:) 请发布您的答案,以便我接受。

标签: wpf grid layouttransform


【解决方案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 的​​更改通知(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
}

【讨论】:

  • 我确实复制了您的项目,但(目前)无法让它与 UserControl 一起使用。不过我还是会看看的……
  • 根据我的观察,这与绑定有关。当您在用户控件的 xaml 中简单地使用继承的 Text 属性(而不是绑定到 MyText)时,它可以正常工作。
  • MyText 属性是由 Visual Studio Snippet propdp 创建的简单 DependencyProperty。我找不到任何相关的配置。
  • 顺便说一句,我认为将Name="CustomRotatedTextBlock"Text="{Binding ElementName=CustomRotatedTextBlock,Path=MyText}" 替换为DataContext={Binding RelativeSource={RelativeSource Self}}"Text="{Binding Path=MyText}" 会更好。
【解决方案2】:

你试过UpdateLayout 吗?打开窗口后尝试为网格更新布局

【讨论】:

  • -1 这实在是太繁重了。这个问题有一个更简单的原因。
猜你喜欢
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 2014-11-06
  • 2016-07-15
  • 1970-01-01
  • 2022-12-06
  • 2021-10-01
  • 1970-01-01
相关资源
最近更新 更多