簡體   English   中英

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

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

我正在嘗試在 WPF 中構建一個 UI 布局,該布局可以隨 window 的大小進行縮放。 這個想法是在左邊有一些控件,在右邊有一些控件,在中間有一行文本。 如果右邊的文本行被裁剪就可以了。 最主要的是所有控件的縱橫比都保持不變。

那部分工作正常。 問題是文本的中心線似乎有一個最小高度; 低於這個高度,它將開始垂直剪裁。 如果我將 window 做得很薄,我希望文本繼續縮小。 即使在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>

問題是您喜歡水平發生的Viewbox剪輯,但不希望垂直剪輯。 換句話說,您需要UniformToFill直到水平剪裁停止,然后您想切換到Uniform

要獲得這兩種行為,您需要Viewbox的替代方法。 不久前,我在 Stack Overflow 的這個名為ViewboxPanel的答案中寫了一個這樣的布局元素的原型:

我剛剛對其進行了測試,至少對於您的示例 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