如何:创建具有访问键和文本换行的控件

此示例演示如何创建具有访问键并支持文本换行的控件。 该示例使用控件 Label 来说明这些概念。

示例:

向标签添加文字换行

控件 Label 不支持文本换行。 如果您需要一个可以跨多行换行的标签,可以在标签内部嵌套一个支持文本换行的元素。 以下示例演示如何使用 a TextBlock 创建包装多行文本的标签。

<Label Width="200" HorizontalAlignment="Left">
  <TextBlock TextWrapping="WrapWithOverflow">
    A long piece of text that requires text wrapping
    goes here.
  </TextBlock>
</Label>

向标签添加访问键和文本换行

如果您需要一个具有访问键(助记键)的 Label,请使用包含在 AccessText 内部的 Label 元素。

控件(如LabelButton、、RadioButtonCheckBoxMenuItemTabItemExpanderGroupBox具有默认控件模板。 这些模板包含一个 ContentPresenter。 可以在该控件上 ContentPresenter 设置的属性之一为 RecognizesAccessKey=“true”,可用于为控件指定访问键。

以下示例展示了如何创建一个具有访问密钥并支持文本换行的 Label。 为了启用文本换行,该示例设置 TextWrapping 属性并使用下划线字符指定访问键。 (紧跟下划线字符的字符是访问键。

<TextBox Name="textBox1" Width="50" Height="20"/>
<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>

另请参阅