wpf - 如何将粗体文本和 AccessText 添加到标签或 TextBlock?

标签 wpf xaml

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

Enter this preparer's info: [ComboBox]

Alt+E 是聚焦 ComboBox 的访问键,按下 Alt 时,文本中的 E 应该带有下划线。

我可以获得轻松工作的访问 key :

<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 似乎对其没有任何影响。

示例: alt text

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

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

我在这里缺少什么?看来必须有一种更简单的方法。

最佳答案

变化不大,但怎么样

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

关于wpf - 如何将粗体文本和 AccessText 添加到标签或 TextBlock?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4000691/

相关文章:

WPF TabItem 标题样式

silverlight - 打开 XAML 文件时 Visual Studio 2010 崩溃

c# - 在 variablesizedwrapgrid 代码后面设置 TextBlock 的宽度和高度无法正确显示

c# - 尝试在循环中设置 CategoryIndex 时出现 IndexOutOfRangeException

c# - 计算富文本框中所有单词的最有效方法是什么?

c# - 列表框仅显示一项

wpf - 如何将 "Red"这样的字符串转换为 System.Windows.Media.Color?

c# - 从同一列检索多个值的有效方法

c# - TextBox.TextChanged 事件在 Windows Phone 7 模拟器上触发两次

c# - 从应用程序数据本地文件夹在 UWP 中的 xaml 中的 WebView 中加载 Html 文件