我正在努力將一些捷徑鍵綁定到我的WPF按鈕。的按鈕是動態建立通過使用以下代碼[只有一個片段 - ,但應足以]:Button(s)KeyBinding [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>
請問誰能幫我解決這個問題? 非常感謝提前!
親切的問候,
嗨費利斯,非常感謝您的答覆。我會試一試,讓你知道!親切的問候 –
這比在編寫每個UI組件的代碼背後編寫處理程序更可重用 –