开发者

Problems binding keys in MenuItem's with IsCheckable="True", why?

开发者 https://www.devze.com 2023-01-26 06:39 出处:网络
This MenuItem does not switch to checked when I press Alt+S, why? <Menu> <MenuItem Header=\"_Other\">

This MenuItem does not switch to checked when I press Alt+S, why?

<Menu>
   <MenuItem Header="_Other">
      <MenuItem
          Header="_Show Expanded Names ?"
          IsCheckable="True"
          StaysOpenOnClick="True"
          InputGestureText="Alt+S"
          IsChecked="{Binding ShowExpandedName}" />
   </MenuItem>
</Menu>

NOTE: ShowExpandedName is defined as follows in the DataContext. The MenuItem is checked correctly when I click on it with the mouse.

bool _ShowExpandedName;
public bool ShowExpandedName
{
     get { return _ShowExpandedName; }
     set
          {
              if (value != _ShowExpandedName)
              {
                  _ShowExpandedName = v开发者_如何学运维alue;
                  this.NotifyPropertyChanged("ShowExpandedName");
              }
          }
}


you need to make ShowExpandedName a dependencyProperty so that the change in its value is reported to the menuItem

see there:

http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.aspx


I think I found:

http://msdn.microsoft.com/en-en/library/system.windows.controls.menuitem.inputgesturetext%28VS.85%29.aspx

Notes

This property does not associate the input gesture with the menu item it simply adds text to the menu item. For information on how to associate a command with a menu item see: Command.


The "_" trick is supposed to work on visible menu items. In this case I'd have to do Alt-O first to open the "_Other" submenu and then press S to choose the item "Show Expanded Names ?". I guess the moral of this is that using "" is not the same as adding a key binding to the command itself!

0

精彩评论

暂无评论...
验证码 换一张
取 消