我有一些文本在運行時顯示在文本塊中。我想 字體大小是最大的,它可以填充給定的區域,即 。我認爲我已經正確設置了「autosize」的文本塊設置,並嘗試增加字體大小,直到文本塊大於 其父項然後將字體大小減少1.問題是我不能 獲取控件重新繪製/重新計算其大小。WPF TextBlock字體調整大小以填充網格中的可用空間
是否有更好的方法來做到這一點?或者有什麼方法可以讓我的方法奏效?
我有一些文本在運行時顯示在文本塊中。我想 字體大小是最大的,它可以填充給定的區域,即 。我認爲我已經正確設置了「autosize」的文本塊設置,並嘗試增加字體大小,直到文本塊大於 其父項然後將字體大小減少1.問題是我不能 獲取控件重新繪製/重新計算其大小。WPF TextBlock字體調整大小以填充網格中的可用空間
是否有更好的方法來做到這一點?或者有什麼方法可以讓我的方法奏效?
包裹TextBlock
內ViewBox
:
<Grid>
<Viewbox>
<TextBlock TextWrapping="Wrap" Text="Some Text" />
</Viewbox>
</Grid>
我發現了一個偉大的方式來做到這一點使用ViewBox
:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Viewbox Grid.Row="0" Grid.Column="0" Stretch="Uniform">
<TextBlock Name="tbTest" Background="Yellow" Text="This is some text" />
</Viewbox>
<ContentControl Grid.Column="0" Grid.Row="2">
<TextBlock>This is some text</TextBlock>
</ContentControl>
</Grid>
1分鐘太晚了。 :^) – Conrad
那麼,它不是一個 「完美」 的答案,但是這是一個快速入侵(您可以將其放入kaxaml並進行測試):
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Height="300" Background="green">
<Viewbox>
<TextBlock Background="red" Text="Hurr"/>
</Viewbox>
</Grid>
</Page>
ViewBox
將放大任何內容以填充其容器。問題是,TextBlock
,雖然它的大小,它的文字,填充頂部和底部,你不能擺脫(沒有做一些繁重的工作)。這可能會讓你更接近你想要的東西,壽。
我有同樣的問題。您可以使用此調整大小字體大小的文本塊填充區域時,它有溢出。
<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
<TextBlock Text="{Binding Path=Title}" HorizontalAlignment="Center"/>
</Viewbox>
哇,我花了幾個小時試圖找到一個類似的問題的解決方案,使用測量方法和寬度,ActualWidth,RenderWidth,但沒有任何工作ahahahahahahah 我從來沒有發現「DownOnly」屬性,謝謝! :) – Sergio0694
的WPF ViewBox
控制可以長/其內容的可用空間縮小。
只需將您的TextBlock
放在ViewBox
以內;
<Viewbox Stretch="Uniform" Width="50" Height="50">
<TextBlock Text="Test" />
</Viewbox>
ViewBox
通常由其容器縮放。
爲了確保包裹,你需要設置的TextBlock
<Viewbox StretchDirection="DownOnly" Stretch="Uniform">
<TextBlock MaxWidth="500" TextWrapping="Wrap" FontSize="30" VerticalAlignment="Center"
Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."/>
的
MaxWidth
和/或MaxHeight
這填補所有的橫向空間......但如何做同樣也爲垂直空間?我無法將文本塊文本換行,直到它獲得非常小的字體大小。 – Tobia沒有爲我工作。 – TarmoPikaro
這只是將文本拉伸到所在區域的高度;使它最終成爲巨大的。包裹從未踢過它,它繼續流出我的控制權的右邊緣。 – BrainSlugs83