How to disable mnemonics in WPF MenuItem?

I have dynamic lines that display as a MenuItem header, which sometimes contains "_". WPF treats underscores as mnemonics, but I don't want that. How to disable this?

+6
source share
2 answers

After using all the solutions in the WPF list stream . Skip underscores in lines that don't seem to work on MenuItems, I did this:

public class EscapeMnemonicsStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string str = value as string; return str != null ? str.Replace("_", "__") : value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } 
+5
source

An alternative solution is to put the text of your menu in a TextBox with the configured properties.

If you build MenuItem code in code, it will look like this:

 var menuItem = new MenuItem(); var menuHeader = new Textbox(); menuHeader.Text = "your_text_here"; menuHeader.IsReadOnly = true; menuHeader.Background = Brushes.Transparent; menuHeader.BorderThickness = new Thickness(0); menuItem.Header = menuHeader; menuItem.ToolTip = "your detailed tooltip here"; menuItem.Click += YourEventHandlerHere; yourMenu.Items.Add(menuItem); 

If your menu is in XAML and this is just dynamic text, it will look like this:

 <MenuItem Name="menuDynamic" Click="menuDynamic_Click"> <MenuItem.Header> <TextBox Name="dynamicMenu" Text="With_Underscore" IsReadOnly="True" Background="Transparent" BorderThickness="0" /> </MenuItem.Header> </MenuItem> 

Then your code could dynamically set dynamicMenu.Text = "what_ever"; , when it is necessary.

0
source

Source: https://habr.com/ru/post/957342/


All Articles