如何禁用WPF MenuItem中的助记符?

时间:2021-12-24 03:57:24

I have dynamic strings appear as the MenuItem's header, which sometimes contains '_'. WPF treats the underscores as signs for mnemonics, but I don't want that. How do I disable that?

我有动态字符串显示为MenuItem的标题,有时包含'_'。 WPF将下划线视为助记符的标志,但我不希望如此。我如何禁用它?

2 个解决方案

#1


6  

After trying all the solutions in the thread WPF listbox. Skip underscore symbols in strings, which didn't seem to work on MenuItems, I did this:

在线程WPF列表框中尝试所有解决方案之后。跳过字符串中的下划线符号,这似乎不适用于MenuItems,我这样做:

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();
    }
}

#2


0  

An alternate solution is to put your menu text inside a TextBox with adjusted properties.

另一种解决方案是将菜单文本放在具有调整属性的TextBox中。

If building your MenuItem in code, the it would look like this:

如果在代码中构建MenuItem,它将如下所示:

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 it is just the text that is dynamic, it would look like this:

如果你的菜单是在XAML中,而它只是动态的文本,它将如下所示:

<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 behind could dynamically set dynamicMenu.Text = "what_ever"; when it needed to.

然后你的代码可以动态设置dynamicMenu.Text =“what_ever”;什么时候需要。

#1


6  

After trying all the solutions in the thread WPF listbox. Skip underscore symbols in strings, which didn't seem to work on MenuItems, I did this:

在线程WPF列表框中尝试所有解决方案之后。跳过字符串中的下划线符号,这似乎不适用于MenuItems,我这样做:

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();
    }
}

#2


0  

An alternate solution is to put your menu text inside a TextBox with adjusted properties.

另一种解决方案是将菜单文本放在具有调整属性的TextBox中。

If building your MenuItem in code, the it would look like this:

如果在代码中构建MenuItem,它将如下所示:

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 it is just the text that is dynamic, it would look like this:

如果你的菜单是在XAML中,而它只是动态的文本,它将如下所示:

<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 behind could dynamically set dynamicMenu.Text = "what_ever"; when it needed to.

然后你的代码可以动态设置dynamicMenu.Text =“what_ever”;什么时候需要。