简体   繁体   English

包含 TextBlock 的 Viewbox 似乎具有最小高度

[英]Viewbox containing a TextBlock seems to have minimum height

I'm trying to build a UI layout in WPF that can scale with the size of the window.我正在尝试在 WPF 中构建一个 UI 布局,该布局可以随 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.如果我将 window 做得很薄,我希望文本继续缩小。 Even manually setting the FontSize on the TextBlock doesn't work.即使在TextBlock上手动设置FontSize也不起作用。

Note that the controls on the left and right do not have a minimum width;请注意,左侧和右侧的控件没有最小宽度; they can shrink indefinitely.它们可以无限缩小。

My XAML is here.我的 XAML 在这里。 I'm using .NET 4.0.我正在使用 .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>

The problem is that you like the Viewbox clipping when it occurs horizontally but don't want any clipping vertically.问题是您喜欢水平发生的Viewbox剪辑,但不希望垂直剪辑。 In other words, you want UniformToFill until the horizontal clipping stops and then you want to switch to Uniform .换句话说,您需要UniformToFill直到水平剪裁停止,然后您想切换到Uniform

To get both of these behaviors you need an alternative to Viewbox .要获得这两种行为,您需要Viewbox的替代方法。 A while ago I wrote a prototype of just such a layout element in this Stack Overflow answer called ViewboxPanel :不久前,我在 Stack Overflow 的这个名为ViewboxPanel的答案中写了一个这样的布局元素的原型:

I just tested it, and at least for your sample XAML, I think it does just what you want:我刚刚对其进行了测试,至少对于您的示例 XAML,我认为它可以满足您的需求:

<local:ViewboxPanel VerticalAlignment="Top" HorizontalAlignment="Left" Grid.Column="1" Margin="2">
    <TextBlock>Here is a bunch of text that may prove to be interesting.</TextBlock>
</local:ViewboxPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM