Dot-Net

WPF 標籤中的自動換行符

  • April 4, 2018

WPF 是否可以Label自動將自身拆分為幾行?在下面的範例中,文本在右側被裁剪。

<Window x:Class="..." xmlns="..." xmlns:x="..." Height="300" Width="300">
   <Grid>
       <Label>
           `_Twas brillig, and the slithy toves did gyre and gimble in the wabe:
           all mimsy were the borogoves, and the mome raths outgrabe.
       </Label>
   </Grid>
</Window>

難道我做錯了什麼?

不幸的是,採用其他控制項不是一個好的選擇,因為我需要訪問密鑰的支持。

將 替換LabelTextBlock(have TextWrapping="Wrap"),並調整其控制模板以辨識訪問鍵可能是一種解決方案,但這不是矯枉過正嗎?

編輯:標籤的非標準樣式會破壞蒙皮,所以如果可能的話,我想避免它。

同時使用 Label 和 TextBlock 似乎是正確的答案。 這裡有一個howto,可以展示這個確切的問題。

具體來說,在他們的範例中,要獲取包裝文本和訪問密鑰:

<Label Width="200" HorizontalAlignment="Left"
      Target="{Binding ElementName=textBox1}">
 <AccessText TextWrapping="WrapWithOverflow">
   _Another long piece of text that requires text wrapping
   goes here.
 </AccessText>
</Label>

引用自:https://stackoverflow.com/questions/2478312