11

CAL/Prismを使用して複合アプリケーションを構築しています。メイン領域はタブコントロールで、複数のタイプのビューが含まれています。各ビューには、ウィンドウ上部のツールバーボタンにバインドされた、処理可能なカスタムセットコマンドがあります。以前、非CALアプリでコマンドにInputBindingを設定するだけでこれを実行しましたが、CALモジュールのソースコードでそのようなメカニズムを見つけることができませんでした。

Alt私の質問は、ユーザーが+を押したときTに、関連付けられたDelegateCommandオブジェクトがそれを処理するように、キーストロークをビューに接続するための最良の方法は何ですか?ショートカットを接続するのはそれほど難しいことではありません...

4

3 に答える 3

18

参考までに、CommandReferenceクラスは現在、参照できるアセンブリには含まれていませんが、MV-VM プロジェクト テンプレートには含まれています。したがって、テンプレートからアプリケーションをビルドしない場合は、別の場所からクラスを取得する必要があります。サンプルプロジェクトからコピーすることにしました。誰もがこの小さなチャンクに簡単にアクセスできるように以下に含めましたが、MV-VM ツールキットの将来のバージョンでテンプレートが更新されていないか確認してください。

/// <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
}

楽しみ!

于 2009-09-24T13:59:34.740 に答える
14

MVVM ToolkitCommandReferenceには、コマンドへの参照をキーバインドとして使用できるaというクラスがあります。

<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>

これでいけます。

編集:これが書かれて以来、WPF 4.0 はこの特定の問題を修正し、静的リソースの回避策を使用する必要がなくなりました。ViewModel のコマンドは KeyBinding から直接参照できます。

于 2009-09-23T22:43:09.880 に答える
0

これらの記事をチェックしてください: http://coderscouch.com/tags/input%20bindings . それらは役立つはずです。

于 2013-04-10T03:26:51.180 に答える