wpf - Issues with MouseDoubleClick command on TreeViewItem using AttachedProperties -
i trying attach command , commandparameter property treeviewitem can route mouse double click event command in viewmodel. appears double click event never fired.
mousedoubleclick:
namespace visualinspectionconsole.commands { public class mousedoubleclick { public static dependencyproperty commandproperty = dependencyproperty.registerattached( "command", typeof(icommand), typeof(mousedoubleclick), new uipropertymetadata(commandchanged)); public static dependencyproperty commandparameterproperty = dependencyproperty.registerattached( "commandparameter", typeof(object), typeof(mousedoubleclick), new uipropertymetadata(null)); public static void setcommand(dependencyobject target, icommand value) { target.setvalue(commandproperty, value); } public static void setcommandparameter(dependencyobject target, object value) { target.setvalue(commandparameterproperty, value); } public static object getcommandparameter(dependencyobject target) { return target.getvalue(commandparameterproperty); } private static void commandchanged( dependencyobject target, dependencypropertychangedeventargs e) { control control = target control; if (control != null) { if ((e.newvalue != null) && (e.oldvalue == null)) { control.mousedoubleclick += onmousedoubleclick; } else if ((e.newvalue == null) && (e.oldvalue != null)) { control.mousedoubleclick -= onmousedoubleclick; } } } private static void onmousedoubleclick(object sender, routedeventargs e) { control control = sender control; ((icommand) control.getvalue(commandproperty)).execute( control.getvalue(commandparameterproperty)); } } }
tree view code in mainwindow.xaml:
<window ................... xmlns:commands ="clr-namespace:visualinspectionconsole.commands" ................... <treeview width="275" name="tvwwaferlist" scrollviewer.cancontentscroll="true" scrollviewer.verticalscrollbarvisibility="visible" itemssource="{binding treeviewviewmodel.waferlist}" selectedvaluepath="wafer" background="{staticresource accentbrushone}" foreground="black" fontfamily="segoeui" fontweight="bold" margin="1" borderbrush="{staticresource primarybrush}"> <treeview.itemcontainerstyle> <style targettype="{x:type treeviewitem}"> <setter property="foreground" value="{staticresource primarybrush}"/> <setter property="fontweight" value="normal"/> <setter property="commands:mousedoubleclick.command" value="{binding selectwafercommand}"/> <setter property="commands:mousedoubleclick.commandparameter" value="{binding}"/> <style.triggers> <trigger property="isselected" value="true"> <setter property="fontweight" value="bold"/> </trigger> </style.triggers> </style> </treeview.itemcontainerstyle> <treeview.itemtemplate> <hierarchicaldatatemplate itemssource="{binding wafers}"> <textblock text="{binding}" foreground="black"/> </hierarchicaldatatemplate> </treeview.itemtemplate> </treeview>
can tell me why mouse double click not working?
edit: getting error in output window:
system.windows.data error: 40 : bindingexpression path error: 'selectwafercommand' property not found on 'object' ''waferhierarchymodel' (hashcode=4494425)'. bindingexpression:path=selectwafercommand; dataitem='waferhierarchymodel' (hashcode=4494425); target element 'treeviewitem' (name=''); target property 'command' (type 'icommand')
so after reading able work correctly. had change commands:mousedoubleclick.commands value
from
{binding selectwafercommand}"
to
{binding datacontext.treeviewviewmodel.selectwafercommand, relativesource={relativesource findancestor, ancestortype={x:type treeview}}}"
because waferhierarchymodel objects not have selectwafercommand, treeviewviewmodel does. tells execute selectwafercommand found in nearest treeview ancestors datacontext.treeviewviewmodel object.
Comments
Post a Comment