Featured post
c# - WPF/MVVM - how to handle double-click on TreeViewItems in the ViewModel? -
(note - re-post first question got posted under wrong headline: here sorry!)
i have standard wpf treeview , have bound items view model classes.
i wish handle behaviour when items double-clicked (opening documents visual-studio-style).
i can event-handler fire in control housing treeview (xaml shown), how bind specific behaviour on view model classes - e.g. projectviewmodel?
preferable bound icommand-implementer, used elsewhere...
<treeview itemssource="{binding projects}" mousedoubleclick="treeview_mousedoubleclick"> <treeview.itemcontainerstyle> <!-- style binds treeviewitem treeviewitemviewmodel. --> <style targettype="{x:type treeviewitem}"> <setter property="isexpanded" value="{binding isexpanded, mode=twoway}" /> <setter property="isselected" value="{binding isselected, mode=twoway}" /> <setter property="fontweight" value="normal" /> <style.triggers> <trigger property="isselected" value="true"> <setter property="fontweight" value="bold" /> </trigger> </style.triggers> </style> </treeview.itemcontainerstyle> <treeview.resources> <hierarchicaldatatemplate datatype="{x:type implementations:projectviewmodel}" itemssource="{binding children}"> <stackpanel orientation="horizontal"> <image width="16" height="16" margin="3,0" source="images\region.png" /> <textblock text="{binding displayname}" /> </stackpanel> </hierarchicaldatatemplate> <hierarchicaldatatemplate datatype="{x:type implementations:pumpviewmodel}" itemssource="{binding children}"> <stackpanel orientation="horizontal"> <image width="16" height="16" margin="3,0" source="images\state.png" /> <textblock text="{binding name}" /> </stackpanel> </hierarchicaldatatemplate> <datatemplate datatype="{x:type implementations:pumpdesignviewmodel}"> <stackpanel orientation="horizontal"> <image width="16" height="16" margin="3,0" source="images\city.png" /> <textblock text="{binding name}" /> </stackpanel> </datatemplate> </treeview.resources> </treeview>
updating answer bit.
i've tried alot of different approaches , still feel attached behaviors best solution. although might alot of overhead in begining isn't. keep of behaviors icommands
in same place , whenever need support event matter of copy/paste , change event in propertychangedcallback
.
i added optional support commandparameter
.
in designer matter of selecting desired event
you can set either on treeview
, treeviewitem
or other place like.
example. set on treeview
<treeview commandbehaviors:mousedoubleclick.command="{binding yourcommand}" commandbehaviors:mousedoubleclick.commandparameter="{binding}" .../>
example. set on treeviewitem
<treeview itemssource="{binding projects}"> <treeview.itemcontainerstyle> <style targettype="{x:type treeviewitem}"> <setter property="commandbehaviors:mousedoubleclick.command" value="{binding yourcommand}"/> <setter property="commandbehaviors:mousedoubleclick.commandparameter" value="{binding}"/> </style> </treeview.itemcontainerstyle> </treeview>
and here attached behavior mousedoubleclick
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 command = (icommand)control.getvalue(commandproperty); object commandparameter = control.getvalue(commandparameterproperty); command.execute(commandparameter); } }
- Get link
- X
- Other Apps
Comments
Post a Comment