問題描述
使用 IsCheckable="True" 綁定 MenuItem 中的鍵時出現問題,為什麼? (Problems binding keys in MenuItem's with IsCheckable="True", why?)
當我按 Alt+S 時,這個 MenuItem 沒有切換到選中狀態,為什麼?
<Menu>
<MenuItem Header="_Other">
<MenuItem
Header="_Show Expanded Names ?"
IsCheckable="True"
StaysOpenOnClick="True"
InputGestureText="Alt+S"
IsChecked="{Binding ShowExpandedName}" />
</MenuItem>
</Menu>
注意:ShowExpandedName 在 DataContext 中定義如下。當我用鼠標單擊 MenuItem 時,它會被正確檢查。
bool _ShowExpandedName;
public bool ShowExpandedName
{
get { return _ShowExpandedName; }
set
{
if (value != _ShowExpandedName)
{
_ShowExpandedName = value;
this.NotifyPropertyChanged("ShowExpandedName");
}
}
}
## 參考解法 #### 方法 1:
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
方法 2:
I think I found:
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.
方法 3:
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!
(by Nestor、David、David、Nestor)