2010-03-19 141 views
4

自動斷行有可能是一個WPF Label本身自動分割成幾行?在我的下面的例子中,文本被裁剪在右邊。在WPF標籤

<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> 

我做錯了什麼?

以其他控件是不幸的是沒有一個很好的選擇,因爲我需要支持訪問鍵。

與更換Label一個TextBlock(有TextWrapping="Wrap"),並調整其控制模板來識別訪問鍵會也許是一個解決方案,但是是不是矯枉過正?

編輯:具有非標準的風格標籤將打破剝皮,所以我想如果可能的話,以避免它。

+1

使用標籤的TextBlock的,而不是看起來簡單的解決方案。它的工作原理並不過分,並且不會顯着增加代碼的大小。 有時與WPF,它不清楚爲什麼某些事情都一樣,爲什麼有兩個標籤和TextBlock的,當它可能只是在所有相關的情況下使用TextBlock的存在。 – kmontgom 2010-03-19 15:12:27

+1

@kmontgom:確實'TextBlock'和'Label'是不同的野獸:看到一篇不錯的文章http://joshsmithonwpf.wordpress.com/2007/07/04/differences-between-label-and-textblock/。我認爲必須有比替換控件模板更簡單的解決方案(例如,因爲這可能會中斷皮膚)。 – Vlad 2010-03-19 15:15:58

+0

感謝您的鏈接。好點。這告訴我的是,可能應該將分行功能添加到

回答

7

同時使用標籤和TextBlock的一起似乎是正確的答案。 There's a howto located here that demonstrates this exact issue.

具體而言,在他們爲榜樣,以獲得環繞文字以及訪問密鑰:

<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> 
+0

非常感謝,這一定是我需要的。讓我檢查... – Vlad 2010-03-19 15:21:16

+0

確實,它的工作! :-) – Vlad 2010-03-19 15:22:59

+0

@Ben:在你的回答中,s/TextBlock/AccessText/:-) – Vlad 2010-03-19 15:25:15