2010-03-19 143 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