【问题标题】:How can I add bold text and AccessText to a Label or TextBlock?如何将粗体文本和 AccessText 添加到标签或文本块?
【发布时间】:2010-11-12 02:12:15
【问题描述】:

我有一个 WPF 难题。我希望一些文本看起来像这样:

输入此准备者信息: [组合框]

Alt+E是聚焦ComboBox的快捷键,按下Alt时,文本中的E要加下划线。

我可以轻松获得访问密钥:

<Label Target="{Binding ElementName=PreparerComboBox}">
    _Enter this preparer's info:</Label>

但是“准备者”不能加粗,因为标签不支持运行(据我所知)。

我可以在 TextBlock 中轻松地加粗:

<TextBlock>Enter this <Bold>preparer's</Bold> info:</TextBlock>

但是没有定义访问密钥,所以我尝试在 TextBlock 中添加我的 AccessText:

<Label Target="{Binding ElementName=PreparerComboBox}">
    <TextBlock>
        <AccessText>_Enter</AccessText> this <Bold>preparer's</Bold> info:
    </TextBlock>
</Label>

但是 AccessText 并没有与 TextBlock 中的其余文本正确对齐,并且 Margin 似乎对其没有任何影响。

示例:

到目前为止我想出的最好的就是这个怪物

<Label Target="{Binding ElementName=PreparerComboBox}">
    <WrapPanel>
        <AccessText>_E</AccessText>
        <TextBlock>nter this <Bold>preparer's</Bold> info:</TextBlock>
    </WrapPanel>
</Label>

我在这里缺少什么?似乎必须有一个更简单的方法。

【问题讨论】:

  • “没有正确排列”是什么意思?可以发个截图吗?
  • 我知道已经有一段时间了,肯特,但我终于添加了一个截图。
  • 您的访问密钥是否在字母下方显示了下划线?我已在 Windows 中打开此功能(始终显示带下划线的助记符)并且访问键正在工作并适当地设置目标的焦点,但我在访问键的字母下看不到任何下划线。
  • @jamiebarrow 我不记得了。我已经搬到其他地方了,所以我无法再访问该代码来检查了。
  • @ElmoGallen 原来标签的大小/位置剪裁了下划线,这就是为什么我看不到它:)

标签: wpf xaml


【解决方案1】:

变化不大,但怎么样

<Label Target="{Binding ElementName=PreparerComboBox}">
    <StackPanel Orientation="Horizontal">
        <AccessText>_Enter</AccessText>
        <TextBlock xml:space="preserve"> this <Bold>preparer's</Bold> info:</TextBlock>
    </StackPanel>
</Label>

【讨论】:

  • 这样更好。 xml:space="preserve" 很好,但总的来说这几乎和我上面的怪物一模一样。
猜你喜欢
  • 2012-09-02
  • 2014-09-04
  • 2014-10-04
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 2021-07-11
相关资源
最近更新 更多