【发布时间】:2010-10-10 17:25:12
【问题描述】:
我有一个包含菜单和子菜单的应用程序。我已将应用命令附加到一些子菜单项,例如剪切、复制和粘贴。
我还有一些其他菜单项没有应用程序命令。
如何将自定义命令绑定添加到这些子菜单项?
我浏览了this 文章,但无法将事件附加到我的子菜单项。
【问题讨论】:
-
找到这个 youtube 链接。 youtube.com/watch?v=mG4l0AaYBTM
我有一个包含菜单和子菜单的应用程序。我已将应用命令附加到一些子菜单项,例如剪切、复制和粘贴。
我还有一些其他菜单项没有应用程序命令。
如何将自定义命令绑定添加到这些子菜单项?
我浏览了this 文章,但无法将事件附加到我的子菜单项。
【问题讨论】:
我使用放置在 Window1 类(或任何窗口类的名称)之后的静态类,在其中创建 RoutedUICommand 类的实例:
public static class Command {
public static readonly RoutedUICommand DoSomething = new RoutedUICommand("Do something", "DoSomething", typeof(Window1));
public static readonly RoutedUICommand SomeOtherAction = new RoutedUICommand("Some other action", "SomeOtherAction", typeof(Window1));
public static readonly RoutedUICommand MoreDeeds = new RoutedUICommand("More deeds", "MoreDeeeds", typeof(Window1));
}
在窗口标记中添加一个命名空间,使用 Window1 类所在的命名空间:
xmlns:w="clr-namespace:NameSpaceOfTheApplication"
现在我可以为命令创建绑定,就像为应用程序命令创建绑定一样:
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Open" Executed="CommandBinding_Open" />
<CommandBinding Command="ApplicationCommands.Paste" Executed="CommandBinding_Paste" />
<CommandBinding Command="w:Command.DoSomething" Executed="CommandBinding_DoSomething" />
<CommandBinding Command="w:Command.SomeOtherAction" Executed="CommandBinding_SomeOtherAction" />
<CommandBinding Command="w:Command.MoreDeeds" Executed="CommandBinding_MoreDeeds" />
</Window.CommandBindings>
并在菜单中使用绑定,例如:
<MenuItem Name="Menu_DoSomething" Header="Do Something" Command="w:Command.DoSomething" />
【讨论】:
Command.DoSomething)必须是 public,否则您将收到以下异常 “CommandConverter 无法从 System.String 转换”。我假设这是因为管理命令的代码可能位于不同的程序集中,因此看不到 internal 属性。我上周刚开始学习 WPF :D
与其在静态类中定义它们,不如直接在 XAML 中声明命令。示例(改编自 Guffas 的好示例):
<Window.Resources>
<RoutedUICommand x:Key="DoSomethingCommand" Text="Do Something" />
<RoutedUICommand x:Key="DoSomethingElseCommand" Text="Do Something Else" />
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource DoSomethingCommand}" Executed="CommandBinding_DoSomething" />
<CommandBinding Command="{StaticResource DoSomethingElseCommand}" Executed="CommandBinding_DoSomethingElse" />
</Window.CommandBindings>
...
<MenuItem Name="Menu_DoSomething" Header="Do Something" Command="{StaticResource DoSomethingCommand}" />
【讨论】:
CommandBinding_DoSomething和CommandBinding_DoSomethingElse是C#代码。 3./4.有关 RoutedCommands 与常规 Button_Click 绑定的优势的一般性讨论,请参阅joshsmithonwpf.wordpress.com/2008/03/18/…(“谁在乎?”部分)。
我知道我的回答为时已晚,但我希望它对未来有所帮助。
我喜欢 Guffa 和 Heinzi 的答案,但您只能使用一个命令来实现上一个结果。 我通常使用Help命令
<Window.CommandBindings>
<CommandBinding Command="{StaticResource Help}" Executed="HelpExecuted" />
</Window.CommandBindings>
我在每次调用时都使用 CommandParametr,例如
<Window.InputBindings>
<KeyBinding Command="{StaticResource Help}" Key="A" Modifiers="Ctrl" CommandParameter="Case1"/>
<KeyBinding Command="{StaticResource Help}" Key="B" Modifiers="Ctrl" CommandParameter="Case2"/>
<KeyBinding Command="{StaticResource Help}" Key="C" Modifiers="Ctrl" CommandParameter="Case3"/>
<KeyBinding Command="{StaticResource Help}" Key="D" Modifiers="Ctrl" CommandParameter="Case4"/>
<MouseBinding Command="{StaticResource Help}" MouseAction="LeftDoubleClick" CommandParameter="Case5" />
</Window.InputBindings>
或
<Button Command="Help" CommandParameter="Case6" Content="Button">
<Button.InputBindings>
<KeyBinding Command="{StaticResource Help}" Gesture="Ctrl+D" CommandParameter="Case7"/>
</Button.InputBindings>
</Button>
在cs文件中
private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
string str = e.Parameter as string;
switch (str)
{
case null://F1 Pressed default Help
//Code
break;
case "Case1":
//Code
break;
case "Case2":
//Code
break;
case "Case3":
//Code
break;
case "Case4":
break;
case "Case5":
//Code
break;
case "Case6":
//Code
break;
case "Case7":
//Code
break;
}
e.Handled = true;
}
如果你使用的是 MVVM 模式
private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
string str = e.Parameter as string;
Mvvm_Variable.Action(Input: str);
e.Handled = true;
}
并将开关移至 ViewModule 站点。和 Action 是同一个 ViewModule 类中的一个方法。
【讨论】:
RoutedUICommand。此类具有Text 属性,从中获取文本以自动填充调用该命令的菜单条目(并且可以在其他地方使用 - 我经常通过共享模板将按钮文本绑定到它)。如果您使用一个命令(例如,“帮助”),所有菜单项都将具有在Help.Text 中定义的同一段文本。根据您喜欢的架构,这可能不是什么大问题,但我认为这是需要注意的事情。