为什么 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
I have a Grid similar to this:
<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>
and myNamespace:MyRotatedTextBlock is a custom WPF control like this:
<TextBlock Text="{Binding MyText}"
HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.LayoutTransform>
<RotateTransform Angle="90"/>
</TextBlock.LayoutTransform>
</TextBlock>
The problem is when I open the window, I can't see the second row which contains the rotated text. But if I replace the Height
of the second row (which is set to "Auto"
) with "100"
then I can see that the second row is shown and it contains MyHeader2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以像这样从 TextBlock (而不是 userControl)派生:
然后只需使用 TextBlock 中的 Text 属性,如下所示:
编辑
这样它也可以用作 UserControl (因为绑定的元素名明确指定为用户控件的名称):
然后我在代码后面使用 INotifyPropertyChanged 的更改通知(WPF 严重依赖它;)
You can also derive from TextBlock (instead of userControl) like this:
Then just use the Text property from the TextBlock like this:
EDIT
This way it works as a UserControl as well (because the elementname of the binding is specified explicitly to the user control's name):
Then i code behind use Change Notification by INotifyPropertyChanged (which WPF relies heavy upon ;)
您尝试过 UpdateLayout 吗?打开窗口后尝试网格的 UpdateLayout
Did you try
UpdateLayout
? try UpdateLayout for the grid after opening the window