JMc*_*iel 11 wpf command prism keyboard-shortcuts composite
我正在使用CAL/Prism构建复合应用程序.主区域是一个选项卡控件,其中包含多种类型的视图.每个视图都有一个可以处理的自定义设置命令,这些命令绑定到窗口顶部的工具栏按钮.我之前在非CAL应用程序中通过简单地在命令上设置InputBinding来完成此操作,但我无法在CAL模块的源代码中找到任何此类机制.
我的问题是,将键击连接到我的视图的最佳方法是什么,这样当用户按下Alt+时T,关联的DelegateCommand对象会处理它?连接快捷方式不是那么困难......
JMc*_*iel 17
仅供参考,CommandReference类当前未包含在您可以引用的程序集中,但包含在MV-VM项目模板中.因此,如果您不从模板构建应用程序,那么您必须从其他地方获取该类.我选择从示例项目中复制它.我将它包含在下面,以便每个人都能轻松访问这一小块优点,但请确保在未来版本的MV-VM Toolkit中检查模板的更新.
/// <summary>
/// This class facilitates associating a key binding in XAML markup to a command
/// defined in a View Model by exposing a Command dependency property.
/// The class derives from Freezable to work around a limitation in WPF when data-binding from XAML.
/// </summary>
public class CommandReference : Freezable, ICommand
{
public CommandReference( )
{
}
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( "Command", typeof( ICommand ), typeof( CommandReference ), new PropertyMetadata( new PropertyChangedCallback( OnCommandChanged ) ) );
public ICommand Command
{
get { return (ICommand)GetValue( CommandProperty ); }
set { SetValue( CommandProperty, value ); }
}
#region ICommand Members
public bool CanExecute(object parameter)
{
if (Command != null)
return Command.CanExecute( parameter );
return false;
}
public void Execute(object parameter)
{
Command.Execute( parameter );
}
public event EventHandler CanExecuteChanged;
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CommandReference commandReference = d as CommandReference;
if (commandReference != null)
{
ICommand oldCommand = e.OldValue as ICommand;
if (oldCommand != null)
oldCommand.CanExecuteChanged -= commandReference.CanExecuteChanged;
ICommand newCommand = e.NewValue as ICommand;
if (newCommand != null)
newCommand.CanExecuteChanged += commandReference.CanExecuteChanged;
}
}
#endregion
#region Freezable
protected override Freezable CreateInstanceCore( )
{
return new CommandReference();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
请享用!
And*_*mes 14
该MVVM工具包有一个叫做类CommandReference
,让你使用一个参考的命令作为键绑定.
<Window ...
xmlns:toolkit="clr-namespace:CannotRememberNamspace;assembly=OrTheAssembly"
>
<Window.Resources>
<toolkit:CommandReference
x:Key="ExitCommandReference"
Command="{Binding ExitCommand}" />
</Window.Resources>
<Window.InputBindings>
<KeyBinding Key="X"
Modifiers="Control"
Command="{StaticResource ExitCommandReference}" />
</Window.InputBindings>
</Window>
Run Code Online (Sandbox Code Playgroud)
这样就行了.
编辑:由于这是编写的,WPF 4.0修复了这个特定问题,您不再需要使用静态资源解决方法.您可以直接从KeyBinding引用viewmodel中的命令.
归档时间: |
|
查看次数: |
8802 次 |
最近记录: |