wpf - C+Ctrl KeyBinding is not causing copying to happen -
i have set listbox
so:
<listbox itemssource="{binding logs, mode=oneway}" x:name="loglistview"> <listbox.itemtemplate> <datatemplate> <textblock text="{binding path=.}"> <textblock.inputbindings> <keybinding key="c" modifiers="ctrl" command="copy"/> </textblock.inputbindings> <textblock.commandbindings> <commandbinding command="copy" executed="keycopylog_executed" canexecute="copylog_canexecute"/> </textblock.commandbindings> <textblock.contextmenu> <contextmenu> <menuitem command="copy"> <menuitem.commandbindings> <commandbinding command="copy" executed="menucopylog_executed" canexecute="copylog_canexecute"/> </menuitem.commandbindings> </menuitem> </contextmenu> </textblock.contextmenu> </textblock> </datatemplate> </listbox.itemtemplate> </listbox>
as can see, in template, each textblock
has context menu allows user copy text. works.
also in textblock
there keybinding
ctrl+c , commandbinding
copy. when press ctrl+c method keycopylog_executed
not executed. i've checked debugger.
how should binding keys textblock
?
there couple of problems here.
first of all, keybindings
work if focused element located inside element keybindings defined. in case have listboxitem
focused, keybindings
defined on child element - textblock
. so, defining keybindings
on textblock
not work in case, since textblock cannot receive focus.
second of all, need know copy, need pass selected log item parameter copy
command.
furthermore, if define contextmenu
on textblock
element opened if right-click on textblock
. if click on other part of list item, not open. so, need define contextmenu
on list box item itself.
considering of that, believe trying can done in following way:
<listbox itemssource="{binding logs, mode=oneway}" x:name="loglistview" issynchronizedwithcurrentitem="true"> <listbox.inputbindings> <keybinding key="c" modifiers="ctrl" command="copy" commandparameter="{binding logs/}" /> </listbox.inputbindings> <listbox.commandbindings> <commandbinding command="copy" executed="copylogexecuted" canexecute="canexecutecopylog" /> </listbox.commandbindings> <listbox.itemcontainerstyle> <style targettype="{x:type listboxitem}"> <setter property="contextmenu"> <setter.value> <contextmenu> <menuitem command="copy" commandparameter="{binding}" /> </contextmenu> </setter.value> </setter> </style> </listbox.itemcontainerstyle> <listbox.itemtemplate> <datatemplate> <textblock text="{binding}" /> </datatemplate> </listbox.itemtemplate> </listbox>
here define keybinding
on listbox
, specify commandparameter
selected list box item (log entry).
commandbinding
defined @ listbox
level , single binding both right click menu , keyboard shortcut.
the contextmenu
define in style listboxitem
, bind commandparameter
data item represented listboxitem
(log entry).
the datatemplate
declares textblock
binding current data item.
and finally, there 1 handler copy
command in code-behind:
private void copylogexecuted(object sender, executedroutedeventargs e) { var logitem = e.parameter; // copy log item clipboard } private void canexecutecopylog(object sender, canexecuteroutedeventargs e) { e.canexecute = true; }
Comments
Post a Comment