【问题标题】:WPF listbox. Skip underscore symbols in stringsWPF 列表框。跳过字符串中的下划线符号
【发布时间】:2011-01-05 07:40:07
【问题描述】:

我有一些动态填充项目的 WPF ListBox。像这样:

ListBox.Items.Add
(new ListBoxItem { Content = new CheckBox { IsChecked = true, Content = "string_string"} );

问题在于复选框内容。它在 GUI 上显示,如“stringstring”...... 如何转义“_”符号? (我动态获取字符串)

【问题讨论】:

  • 你想看下划线还是去掉它?
  • 我想在 GUI 上看到下划线

标签: c# wpf


【解决方案1】:

您可以在 TextBlock 中添加文本并将该 TextBlock 放入您的 Chekbox,TextBlock 不支持 _ 助记符。这就是我的意思,在 xaml 中,但您可以轻松地将其转换为代码:

<CheckBox IsChecked="True">
    <TextBlock>string_string</TextBlock>
</CheckBox>

【讨论】:

  • 我能以某种方式使用“ContentStringFormat”属性转义“_”字符吗?
  • 不,我认为没有任何方法可以通过 ContentStringFormat 转义下划线。即使有办法,转义字符串中的“_”字符意味着遍历字符串中的所有字符,这将比仅使用 TextBlock 慢。
  • 感谢您的回复,我将字符串包装在文本块中
  • 顺便说一句,使用 &lt;ListBox.Resources&gt; &lt;Style TargetType="ContentPresenter"&gt; &lt;Setter Property="RecognizesAccessKey" Value="False" /&gt; &lt;/Style&gt; &lt;/ListBox.Resources&gt; 并没有帮助...
  • ContentStringFormat 确实会有所帮助,如果包含“_”的部分是格式的一部分。我遇到了完全相反的问题并询问了here
【解决方案2】:

CheckBox 的默认模板包含一个 ContentPresenter,其 RecognizesAccessKey 设置为 true。如果内容是字符串(在您的情况下是这样),则 ContentPresenter 创建一个 AccessText 元素来显示文本。该元素隐藏下划线,直到按下 Alt 键,因为它会将其视为助记符。您可以重新模板 CheckBox,使其 ContentPresenter 的 RecognizesAccessKey 为 false 或更好,但只需提供一个 DataTemplate 作为包含 TextBlock 的 ContentTemplate。如果您不确定内容是否为字符串,那么您可以设置 ContentTemplateSelector 并在代码中提供一个 DataTemplate,其中仅当项目是字符串时才包含 TextBlock。例如

<ListBox xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <ListBox.Resources>
        <DataTemplate DataType="sys:String" x:Key="stringTemplate">
            <TextBlock Text="{Binding}" />
        </DataTemplate>
        <Style TargetType="CheckBox">
            <Setter Property="ContentTemplate" Value="{StaticResource stringTemplate}" />
        </Style>
    </ListBox.Resources>
    <ListBoxItem>
        <CheckBox Content="A_B" ContentTemplate="{StaticResource stringTemplate}"/>
        <!-- Or use the implicit style to set the ContentTemplate -->
        <CheckBox Content="A_B" />
    </ListBoxItem>
</ListBox>

【讨论】:

  • 如果你得到一个运行时异常,告诉你在 StaticResource 中找不到模板名称(而设计器完美地处理它),只需从 DataTemplate 中删除 DataType 属性。不知道为什么它在那里以及为什么它不起作用,但这对我有帮助。否则,我喜欢这个解决方案,因为它似乎是 WPF 中摆脱列标题中那些荒谬的访问键的最短方法。
【解决方案3】:

使用双下划线字符串__string,因为在 WPF 中,_ 是助记符。

更好的是,只需在 xaml 中解决此问题并在您的视图模型(或代码隐藏)中创建一个集合。

【讨论】:

  • 没有办法以其他方式禁用此功能?
  • 你可以使用 listBox1.Items.Add(new ListBoxItem { Content = new CheckBox { IsChecked = true, Content = ("string_string").Replace("", "_" )} });反而。这不涉及对实际字符串的修改。
  • 然后如果我想在一些代码中取回字符串,我必须记住再做一个替换......认为 texblocks 是这种方式最优雅的解决方案
  • 是的,文本块方法更有效。
【解决方案4】:

我在 DataGrid 中遇到了同样的问题。与 AndrewS 类似,我为 TextBlock 添加了一个样式,但没有使用 DataTemplate 或 ContentTemplate。这样应用了 ColumnHeaderStyle 中的设置器:) 但是,此解决方案仅适用于单个下划线,例如。 “a_b”,但不是“a__b”。

<DataGrid>
    <DataGrid.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="{Binding}"/>
        </Style>
    </DataGrid.Resources>

    <DataGrid.ColumnHeaderStyle>
        <!-- my setters here do not get overridden -->
    </DataGrid.ColumnHeaderStyle>
</DataGrid>

【讨论】:

    猜你喜欢
    • 2015-04-17
    • 2020-12-22
    • 2011-12-02
    • 1970-01-01
    • 2013-09-20
    • 2015-12-08
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多