商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文

2、四个小问题:
(1)如何指定Command Sources?
XAML:(请将“ApplicationCommands.Properties”换成对应的ApplicationCommands属性值,比如:
ApplicationCommands.Copy)
<StackPanel>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Command="ApplicationCommands.Properties" />
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
同等的C#代码:
StackPanel cmdSourcePanel = new StackPanel();
ContextMenu cmdSourceContextMenu = new ContextMenu();
MenuItem cmdSourceMenuItem = new MenuItem();

cmdSourcePanel.ContextMenu = cmdSourceContextMenu;
cmdSourcePanel.ContextMenu.Items.Add(cmdSourceMenuItem);

cmdSourceMenuItem.Command = ApplicationCommands.Properties;

 

(2)如何指定快捷键?
XAML代码:

<Window.InputBindings>
<KeyBinding Key="B"
Modifiers="Control"
Command="ApplicationCommands.Open" />
</Window.InputBindings>

C#代码:

KeyGesture OpenKeyGesture = new KeyGesture(
Key.B,
ModifierKeys.Control);

KeyBinding OpenCmdKeybinding = new KeyBinding(ApplicationCommands.Open,OpenKeyGesture);
this.InputBindings.Add(OpenCmdKeybinding);
//也可以这样(下面一句与上面两句的效果等同):
//ApplicationCommands.Open.InputGestures.Add(OpenKeyGesture);



(3)如何Command Binding?

XAML代码:

<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Open"
Executed="OpenCmdExecuted"
CanExecute="OpenCmdCanExecute"/>
</Window.CommandBindings>
C#代码:
CommandBinding OpenCmdBinding = new CommandBinding(
ApplicationCommands.Open,
OpenCmdExecuted,
OpenCmdCanExecute);

this.CommandBindings.Add(OpenCmdBinding);


1 2 3 4
©版权所有。未经许可,不得转载。
[责任编辑:铭娅]
[an error occurred while processing this directive]