这就是我过去解决类似问题的方法 - 我希望它对人们有用(这种一般逻辑可以应用于广泛的 Avalon 编辑器相关问题)......
实际发生的情况可能是 Avalon 的错误(结合ListItem
等)。它弄乱了鼠标处理,我猜测焦点(应该在TextArea
for 命令和CanExecute
工作上。
这mouse handling
就是问题所在 - 就好像你只是按下windows
context menu
键它会弹出一个带有启用命令的常规菜单。Avalon 编辑器有一个复杂的鼠标/键处理(很难做一个好的编辑器)——在键盘上它focus
在 TextArea 上做了一个明确的''。您还可以通过在
实际
CanCutOrCopy
处理. 对于“键盘”菜单,它首先进入那里,然后弹出。对于“鼠标”,它会弹出 - 然后在退出时检查(进入该方法)。那都是错的!Editing/EditingCommandHandler.cs
ApplicationCommands.Copy
CanExecute
还有勘误表...
您自己的命令没有问题,只需正常公开您的命令,一切都应该工作。
对于ApplicationCommands
(即RoutedCommand
)它没有正确连接 - 并且Execute
,CanExecute
不要去它应该去的地方,即TextArea
. 要更正您需要rewire
将命令放入自己的包装器中 - 并且基本上调用 TextArea 处理 - 这只是几行代码,但这是必要的步骤(我认为没有更“漂亮”的解决方案,没有修复 Avalon 代码——这可能很痛苦,我从来没有想过)。
(全部基于您的示例 - 填写我遗漏的空白)您的 XAML:
<Window.Resources>
<DataTemplate DataType="{x:Type my:myClass}">
<StackPanel>
<my:AvalonTextEditor x:Name="xmlMessage" SyntaxHighlighting="XML" ShowLineNumbers="True" EditText="{Binding text}" >
<my:AvalonTextEditor.ContextMenu>
<ContextMenu x:Name="mymenu1">
<ContextMenu.Resources>
<Style TargetType="MenuItem">
<Setter Property="CommandParameter" Value="{Binding Path=., RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
</Style>
</ContextMenu.Resources>
<MenuItem Header="My Copy" Command="{Binding CopyCommand}" />
<MenuItem Header="My Paste" Command="{Binding PasteCommand}" />
<MenuItem Header="My Cut" Command="{Binding CutCommand}" />
<MenuItem Header="My Undo" Command="{Binding UndoCommand}" />
<MenuItem Header="My Redo" Command="{Binding RedoCommand}" />
<Separator />
<MenuItem Command="Undo" />
<MenuItem Command="Redo" />
<Separator/>
<MenuItem Command="Cut" />
<MenuItem Command="Copy" />
<MenuItem Command="Paste" />
</ContextMenu>
</my:AvalonTextEditor.ContextMenu>
</my:AvalonTextEditor>
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel>
<DockPanel>
<ListView ItemsSource="{Binding collection}" />
<ContentControl Content="{Binding mc}" />
</DockPanel>
</StackPanel>
背后的代码 - 视图模型:(
注意:我留下了你所说的命名 - 但请不要使用小型大写字母作为道具 :)
public class MyViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public MyViewModel()
{
collection = new ObservableCollection<myClass>(new[]
{
new myClass{ text = "some more test - some more test - some more test - some more test - some more test - some more test - some more test - some more test - some more test - " },
new myClass{ text = "test me test me = test me test me = test me test me = test me test me = test me test me = test me test me = " },
new myClass{ text = "test again - test again - test again - test again - test again - " },
new myClass{ text = "test again - test again - " },
new myClass{ text = "test again - " },
new myClass{ text = "test" },
});
mc = new myClass();
}
public ObservableCollection<myClass> collection { get; set; }
public myClass mc { get; set; }
}
public class myClass
{
public string text { get; set; }
AvalonRelayCommand _copyCommand;
public AvalonRelayCommand CopyCommand
{ get { return _copyCommand ?? (_copyCommand = new AvalonRelayCommand(ApplicationCommands.Copy) { Text = "My Copy" }); } }
AvalonRelayCommand _pasteCommand;
public AvalonRelayCommand PasteCommand
{ get { return _pasteCommand ?? (_pasteCommand = new AvalonRelayCommand(ApplicationCommands.Paste) { Text = "My Paste" }); } }
AvalonRelayCommand _cutCommand;
public AvalonRelayCommand CutCommand
{ get { return _cutCommand ?? (_cutCommand = new AvalonRelayCommand(ApplicationCommands.Cut) { Text = "My Cut" }); } }
AvalonRelayCommand _undoCommand;
public AvalonRelayCommand UndoCommand
{ get { return _undoCommand ?? (_undoCommand = new AvalonRelayCommand(ApplicationCommands.Undo) { Text = "My Undo" }); } }
AvalonRelayCommand _redoCommand;
public AvalonRelayCommand RedoCommand
{ get { return _redoCommand ?? (_redoCommand = new AvalonRelayCommand(ApplicationCommands.Redo) { Text = "My Redo" }); } }
}
(注意:只需连接Window.DataContext
到视图模型,就像你做的那样)
包装需要两个自定义类。
public class AvalonTextEditor : TextEditor
{
#region EditText Dependency Property
public static readonly DependencyProperty EditTextProperty =
DependencyProperty.Register(
"EditText",
typeof(string),
typeof(AvalonTextEditor),
new UIPropertyMetadata(string.Empty, EditTextPropertyChanged));
private static void EditTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
AvalonTextEditor editor = (AvalonTextEditor)sender;
editor.Text = (string)e.NewValue;
}
public string EditText
{
get { return (string)GetValue(EditTextProperty); }
set { SetValue(EditTextProperty, value); }
}
#endregion
#region TextEditor Property
public static TextEditor GetTextEditor(ContextMenu menu) { return (TextEditor)menu.GetValue(TextEditorProperty); }
public static void SetTextEditor(ContextMenu menu, TextEditor value) { menu.SetValue(TextEditorProperty, value); }
public static readonly DependencyProperty TextEditorProperty =
DependencyProperty.RegisterAttached("TextEditor", typeof(TextEditor), typeof(AvalonTextEditor), new UIPropertyMetadata(null, OnTextEditorChanged));
static void OnTextEditorChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
ContextMenu menu = depObj as ContextMenu;
if (menu == null || e.NewValue is DependencyObject == false)
return;
TextEditor editor = (TextEditor)e.NewValue;
NameScope.SetNameScope(menu, NameScope.GetNameScope(editor));
}
#endregion
public AvalonTextEditor()
{
this.Loaded += new RoutedEventHandler(AvalonTextEditor_Loaded);
}
void AvalonTextEditor_Loaded(object sender, RoutedEventArgs e)
{
this.ContextMenu.SetValue(AvalonTextEditor.TextEditorProperty, this);
}
}
public class AvalonRelayCommand : ICommand
{
readonly RoutedCommand _routedCommand;
public string Text { get; set; }
public AvalonRelayCommand(RoutedCommand routedCommand) { _routedCommand = routedCommand; }
public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } }
public bool CanExecute(object parameter) { return _routedCommand.CanExecute(parameter, GetTextArea(GetEditor(parameter))); }
public void Execute(object parameter) { _routedCommand.Execute(parameter, GetTextArea(GetEditor(parameter))); }
private AvalonTextEditor GetEditor(object param)
{
var contextMenu = param as ContextMenu;
if (contextMenu == null) return null;
var editor = contextMenu.GetValue(AvalonTextEditor.TextEditorProperty) as AvalonTextEditor;
return editor;
}
private static TextArea GetTextArea(AvalonTextEditor editor)
{
return editor == null ? null : editor.TextArea;
}
}
笔记:
EditText
只是一个依赖属性 - 能够bind
文本(你的text
) - 这是阿瓦隆的缺点。这里只是为了好玩,但你可能需要它,所以我把它留在了。
用于AvalonRelayCommand
重新连接应用程序路由命令 - 对于其他内容,请使用您自己的命令实现。这两个类是核心。
您需要使用AvalonTextEditor
TextEditor 而不是 TextEditor - 它只是一个小包装器 - 来连接ContextMenu
(TextEditor
除了其他问题,菜单项suffering
缺乏visual tree
- 而且您无法轻松地从中获得任何控件)。我们需要TextEditor
从CommandParameter
(设置为 a ContextMenu
)获取 ref。这可以只使用一些附件属性(不覆盖 TextEditor)来完成,但这样看起来更干净。
在 XAML 方面 - 只需进行一些小的更改 - 使用包装编辑器 - 并且您拥有每个命令的正确参数的MenuItem
样式injects
(您可以通过其他方式做到这一点,这更好)。
这不是hack
- 我们只是通过手动调用TextArea
命令处理来缩短鼠标处理的缺点。差不多就是这样。
享受!