我知道如何在WPF中设置默认的ApplicationCommands命令,以通过ContextMenu启用简单的剪切,复制和粘贴操作。但是,我需要能够在后面的代码中执行此操作,以便在创建TextBox时可以动态分配命令。
如何在后面的代码中重新创建这个非常简单的WPF代码:
<TextBox x:Name="txtTagName" Style="{StaticResource TextBoxStyle}">
<TextBox.ContextMenu>
<ContextMenu Style="{StaticResource DefaultContextMenuStyle}">
<MenuItem x:Name="cmCut" Header="Cut" Command="ApplicationCommands.Cut" />
<MenuItem x:Name="cmCopy" Header="Copy" Command="ApplicationCommands.Copy" />
<MenuItem x:Name="cmPaste" Header="Paste" Command="ApplicationCommands.Paste" />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
答案 0 :(得分:3)
您可以执行以下操作:
this.cmCut.Command = ApplicationCommands.Cut;
答案 1 :(得分:2)
如何在后面的代码中重新创建这个非常简单的WPF代码
类似的事情,即您以编程方式创建TextBox
和ContextMenu
的实例,并设置与您在XAML标记中设置的相同的属性:
TextBox textBox = new TextBox();
textBox.Style = FindResource("TextBoxStyle") as Style;
ContextMenu cm = new ContextMenu();
cm.Style = FindResource("DefaultContextMenuStyle") as Style;
cm.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut });
cm.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy });
cm.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste });
textBox.ContextMenu = cm;
答案 2 :(得分:0)
在这里找到好的答案:How do I add a custom routed command in WPF?
我想添加自己的菜单项自定义输入,并为菜单项中显示的命令添加适当的文本。解决我问题的方法是为窗口添加命令绑定和输入绑定部分,在那里我可以绑定命令类并输入该命令:
...
CALL apoc.do.when(
$imageReference IS NOT NULL,
' CREATE (ir:ImageReference {
id: apoc.create.uuid(),
name: ref.name,
downloadURL: ref.downloadURL,
createdAt: datetime(),
updatedAt: datetime()
})
CREATE (p)-[:HAS_ATTACHMENT]->(ir)
RETURN ir',
'',
{ref: $imageReference, p: p}
) YIELD value
WITH p, value.ir AS ir
...
然后我可以像这样在我的MenuItems中使用它(请注意,“ InputGestureText”将快捷方式/输入文本添加到MenuItem中):
<Window x:Class="SomeNamespace.MainWindow"
<!--Other stuff here-->
xmlns:local="clr-namespace:SomeNamespace"
mc:Ignorable="d"
Title="MainWindow" Height="544" Width="800">
<Window.CommandBindings>
<CommandBinding Command="local:Commands.SomeCommand" Executed="CommandBinding_SomeCommand" />
<CommandBinding Command="local:Commands.SomeOtherCommand" Executed="CommandBinding_SomeOtherCommand" />
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="local:Commands.SomeCommand" Key="S" Modifiers="Ctrl" />
<KeyBinding Command="local:Commands.SomeOtherCommand" Key="O" Modifiers="Ctrl" />
</Window.InputBindings>
“命令”类的代码(在我的示例中为Commands.cs):
<MenuItem Name="MenuItemSomeCommand" Command="local:Commands.SomeCommand" InputGestureText="Ctrl+S" />
<MenuItem Name="MenuItemSomeOtherCommand" Command="local:Commands.SomeOtherCommand" InputGestureText="Ctrl+O" />
以及MainWindow.xaml.cs中执行绑定命令的代码:
using System.Windows.Input;
namespace SomeNamespace
{
public static class Commands
{
public static readonly RoutedUICommand BuildFiles =
new RoutedUICommand("Some Command", "SomeCommand", typeof(MainWindow));
public static readonly RoutedUICommand BuildFiles =
new RoutedUICommand("Some Other Command", "SomeOtherCommand", typeof(MainWindow));
}
}