2011-08-01 4 views
0

私は、いくつかのショートカットキーを自分のWPFボタンにバインドするのに苦労しています。ここボタンキーバインディング[MVVM]

// for each command defined in the database 
... 
PosCommand cmd = null; // FYI: PosCommand implements ICommand 
if (!string.IsNullOrEmpty(group.AssemblyPath)) 
{ 
    if (System.IO.File.Exists(group.AssemblyPath)) 
    cmd = (PosCommand)Activator.CreateInstance(Assembly.LoadFile(group.AssemblyPath).GetType(group.FullQualifiedName), model); 
} 
else 
{ 
cmd = (PosCommand)Activator.CreateInstance(Assembly.GetExecutingAssembly().GetType(group.FullQualifiedName), model); 
} 
if (cmd == null) 
continue; 

Function function = new Function() 
{ 
Command = cmd, 
Name = group.FunctionName, 
Description = group.Description, 
FunctionCode = group.FunctionCode 
}; 
... 

は、C#コードに結合XAMLの抜粋である:

<itemscontrol x:name="functionList" grid.row="0" itemssource="{Binding GroupFunctions}" xmlns:x="#unknown"> 
    <itemscontrol.itemtemplate> 
     <datatemplate> 
      <groupbox header="{Binding Group}"> 
       <itemscontrol scrollviewer.horizontalscrollbarvisibility="Disabled" itemssource="{Binding Functions}"> 
        <itemscontrol.itemspanel> 
         <itemspaneltemplate> 
          <wrappanel /> 
         </itemspaneltemplate> 
        </itemscontrol.itemspanel> 
        <itemscontrol.itemtemplate> 
         <datatemplate> 
          <Button MinWidth="91" Height="50" Content="{Binding Name}" ToolTip="{Binding Description}" Command="{Binding Command}"/> 
         </datatemplate> 
        </itemscontrol.itemtemplate> 
       </itemscontrol> 
      </groupbox> 
     </datatemplate> 
    </itemscontrol.itemtemplate> 
</itemscontrol> 

- [なく十分なものでなければならないだけスニペット]ボタンは、次のコードを使用して動的に構築されます私はいくつかの重要なキーバインディングをボタンに追加しようとしましたが、何の成功もありませんでしたか? Functionクラスを変更して、ボタンのそれぞれについてModifierとKeyのプロパティを含むようにしました。 ModifierとKeyの値をハードコーディングしても、まだ動作したくないのですか?

<Button.InputBindings> 
    <keybinding modifiers="{Binding Mod}" key="{MyKey}" /> 
</Button.InputBindings> 

誰もこの問題で私を助けてくれませんか? 事前に感謝します!

敬具、

答えて

0

今日の終わりに、私は自分のキーリスナーを書いた。私はKeyEventHandlerを扱っ:

protected override void OnInitialized(EventArgs e) 
{    
    base.OnInitialized(e); 

    this.AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent); 
} 

private void HandleKeyDownEvent(object sender, KeyEventArgs e) 
{ 

} 

基本的には、ICommandsのすべてが、データベース内で定義されており、動的に構築されています。したがって、私はCommandPropertyを使用して複雑なデータ型を追加しました。この複雑なデータ型では、さまざまな情報を格納し、使用するショートカットキーを含めます。したがって、キーリスナーはキーの組み合わせを待ち、それを探してICommandを実行します。

親切にして、

1

私は、カスタム・トリガーでSystem.Windows.Interactivityを使用することをお勧めします。私はexample here on my blogをCaliburn Micro MVVMフレームワークで使っていますが、クロスフレームワークのソリューションだと思います。 System.Windows.Interactivity.dllはBlendに含まれていますが、バイナリ形式で再配布可能です。特別な設定は必要ありません.xcopy展開で十分です。

+0

こんにちはFelice、多くのご返信ありがとうございます。私はそれを試してみましょう、あなたに知らせる!種類について –

+0

これは、あなたが書いたすべてのUIコンポーネントのコードビハインドにハンドラを書くよりも、再利用可能なアプローチです –