Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot see _ (underscore) in WPF content

Tags:

wpf

xaml

A very simple question:

Why I cant see _ (underscore) in WPF content?

For instance the content of

<Label Content="test_t" Name="label2"  />

is shown as "testt" (with the underscore not shown).

like image 404
AliRezza Avatar asked Oct 22 '11 18:10

AliRezza


3 Answers

Labels support mnemonics (i.e. you can use ctrl+(key) to give them focus). You define the mnemonic key using an underscore.

http://www.charlespetzold.com/blog/2006/01/061004.html

If you want to see underscores, replace single underscores with double underscores.

like image 103
Tim Lloyd Avatar answered Oct 31 '22 15:10

Tim Lloyd


This is because Label supports defining a mnemonic based on its content, which is done by prefixing the mnemonic with an underscore (the same thing that happens in Windows Forms with &).

Use a double underscore if you want a literal one to appear:

<Label Content="test__t" Name="label2"  />
like image 28
Jon Avatar answered Oct 31 '22 14:10

Jon


I know im late to the party but I believe that if you don't have the Label associated to a TextBox than you should use a TextBlock instead.

Changing your control to a TextBlock solves this issue since only Label has the mnemonic support

like image 6
Fredrik Avatar answered Oct 31 '22 14:10

Fredrik