包含 TextBlock 的 Viewbox 似乎具有最小高度
我正在尝试在 WPF 中构建一个可以随窗口大小缩放的 UI 布局。这个想法是在左侧有多个控件,在右侧有多个控件,中间有一行文本。如果文本行在右侧被裁剪掉就可以了。最主要的是所有控件的纵横比都保持不变。
那部分工作正常。问题是文本的中心线似乎有一个最小高度;低于此高度,它将开始垂直剪切。如果我让窗口变得很薄,我希望文本不断缩小。即使在 TextBlock
上手动设置 FontSize
也不起作用。
请注意,左侧和右侧的控件没有最小宽度;它们可以无限期地缩小。
我的 XAML 在这里。我正在使用.NET 4.0。
<Window x:Class="TestWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="75" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Viewbox Grid.Column="0" Stretch="UniformToFill" Margin="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">A</Button>
<Button Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">B</Button>
<Button Grid.Column="2" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">C</Button>
<Button Grid.Column="3" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">D</Button>
</Grid>
</Viewbox>
<Viewbox VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Column="1" Stretch="UniformToFill" Margin="2">
<TextBlock>Here is a bunch of text that may prove to be interesting.</TextBlock>
</Viewbox>
<Viewbox Grid.Column="2" Stretch="UniformToFill" Margin="2">
<Button HorizontalAlignment="Center">X</Button>
</Viewbox>
</Grid>
</Window>
I'm trying to build a UI layout in WPF that can scale with the size of the window. The idea is to have a number of controls on the left, a number of controls on the right, and in the middle, have a line of text. It's OK if the line of text is cropped on the right. The main thing is that the aspect ratio of all of the controls is maintained.
That part is working fine. The problem is that the center line of text seems to have a minimum height; below this height, it will start clipping vertically. I want the text to keep shrinking if I make the window very thin. Even manually setting the FontSize
on the TextBlock
doesn't work.
Note that the controls on the left and right do not have a minimum width; they can shrink indefinitely.
My XAML is here. I'm using .NET 4.0.
<Window x:Class="TestWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="75" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Viewbox Grid.Column="0" Stretch="UniformToFill" Margin="2">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">A</Button>
<Button Grid.Column="1" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">B</Button>
<Button Grid.Column="2" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">C</Button>
<Button Grid.Column="3" Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center">D</Button>
</Grid>
</Viewbox>
<Viewbox VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Column="1" Stretch="UniformToFill" Margin="2">
<TextBlock>Here is a bunch of text that may prove to be interesting.</TextBlock>
</Viewbox>
<Viewbox Grid.Column="2" Stretch="UniformToFill" Margin="2">
<Button HorizontalAlignment="Center">X</Button>
</Viewbox>
</Grid>
</Window>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您喜欢
Viewbox
水平剪裁,但不希望任何垂直剪裁。换句话说,您需要UniformToFill
,直到水平剪切停止,然后您想要切换到Uniform
。要获得这两种行为,您需要 Viewbox 的替代方案。不久前,我在 Stack Overflow 答案中编写了一个这样的布局元素的原型,名为
ViewboxPanel
:我刚刚测试了它,至少对于您的示例XAML,我认为它正是你想要的:
The problem is that you like the
Viewbox
clipping when it occurs horizontally but don't want any clipping vertically. In other words, you wantUniformToFill
until the horizontal clipping stops and then you want to switch toUniform
.To get both of these behaviors you need an alternative to
Viewbox
. A while ago I wrote a prototype of just such a layout element in this Stack Overflow answer calledViewboxPanel
:I just tested it, and at least for your sample XAML, I think it does just what you want: