19

On my whole application, I've some underscores (_) which are not displayed.

It's due to the accessor. But how can I disable it? Application wide? I don't have them on labels, textboxes, ...

Thank you

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
J4N
  • 19,480
  • 39
  • 187
  • 340
  • possible duplicate of [Disable WPF label accelerator key (text underscore is missing)](http://stackoverflow.com/questions/40733/disable-wpf-label-accelerator-key-text-underscore-is-missing) – Carl Feb 26 '14 at 16:28

4 Answers4

22

To disable underscores globally for all labels you can override the default template for labels like this:

<Style x:Key="{x:Type Label}"
       TargetType="{x:Type Label}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Label}">
                <Border Background="{TemplateBinding Background}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Padding="{TemplateBinding Padding}"
                        SnapsToDevicePixels="true">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                      RecognizesAccessKey="False"
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled"
                             Value="false">
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

It differs from the default template in this line: RecognizesAccessKey="False".

Put this style in global resources of your application (App.xaml) and your labels will not recognize underscores anymore.

Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
  • Hi! Thank you for your response! I will try that today, but seems to be what I need. Is it mandatory to bind default values? – J4N Feb 28 '11 at 09:24
  • @J4N - You don't need to bind anything. Just put this style in global resources (in App.xaml) and it will be automatically applied to all Labels in your application (unless other style is explicitly specified on a label). – Pavlo Glazkov Feb 28 '11 at 09:49
  • Excuse me, I was speaking about these kind of things: Padding="{TemplateBinding Padding}" Are they mandatory? Because if I've to do that for all components which are supposed to display something, I would like to reduce the amount of code for it – J4N Feb 28 '11 at 14:51
  • @J4N - Yes, it is mandatory as long as you want to preserve the all the functionality of the control. For example, if you remove Padding="{TemplateBinding Padding}" then setting Padding on Labels in your application will not work. – Pavlo Glazkov Feb 28 '11 at 15:23
  • Okay, Can I find somewhere default template? Because I've to customize this for several components – J4N Mar 02 '11 at 08:35
  • @J4N - The default templates are included with Expression Blend installation. You can find them in the following folder: "\SystemThemes\Wpf". – Pavlo Glazkov Mar 02 '11 at 08:50
  • I'm using visual studio to design them, is there any chance to use resources in visual studio? – J4N Mar 03 '11 at 09:24
  • @J4N - Of course you can use the resources in Visual Studio. Just in order to get the default control resource you need to install Expression Blend. Once you got the resources, you can copy them in your project and then you can remove Blend. – Pavlo Glazkov Mar 03 '11 at 12:55
  • This solution is in my opinion the best solution, since adding a second underscore is just a workaround with possible side effects. – Peter van Kekem Dec 04 '14 at 08:20
10

Use two underscores:

name = "__something";
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • I can't use this, theses values comes from the database, and I just can't afford me to do this on every field. And most of my fields are directly bounds on the value of my objects. – J4N Feb 28 '11 at 09:22
  • 4
    Use a value converter to replace underscore with double underscore. Easy. – Aliostad Feb 28 '11 at 11:44
9

One easy solution is to not use <Label>. <TextBox> doesn't mess with underscores.

Jeff Dege
  • 11,190
  • 22
  • 96
  • 165
4

Have you tried doubling the underscores?

David
  • 209
  • 1
  • 2
  • why it is happening that double underscore is showing not single ? – Ahmad Oct 01 '17 at 14:03
  • 2
    @Ahmad because the underscore indicates that the next letter is the access key (used with Alt; eg. the Text "_Ahmad" would allow to access it via Alt + A as indicated by underline). So two underscores are the escape sequence. – Hexo Oct 16 '17 at 12:15