c# - Context Menu not showing when pressing Shift+f10 in GridView Column -
i have listview consist of gridview
xaml
<window x:class="gridviewcontextmenu.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:gridviewcontextmenu" mc:ignorable="d" title="mainwindow" height="350" width="525"> <window.resources> <contextmenu x:key="dynamicperiodcontextmenu" background="{dynamicresource {x:static systemcolors.windowbrushkey}}" datacontext="{binding path=placementtarget.datacontext, relativesource={relativesource self}}"> <menuitem header="open" /> <menuitem header="setascomparativeperiod"/> <separator/> <menuitem header="deleteperiod"/> <menuitem header="properties"/> </contextmenu> </window.resources> <grid> <listview itemssource="{binding cars}"> <listview.view> <gridview> <gridviewcolumn header="name"> <gridviewcolumn.celltemplate> <datatemplate> <grid contextmenu="{staticresource dynamicperiodcontextmenu}"> <textblock text="{binding name}"/> </grid> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> <gridviewcolumn header="model"> <gridviewcolumn.celltemplate> <datatemplate> <textblock text="{binding model}"/> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> <gridviewcolumn header="company"> <gridviewcolumn.celltemplate> <datatemplate> <textblock text="{binding company}"/> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> <gridviewcolumn header="registration"> <gridviewcolumn.celltemplate> <datatemplate> <textblock text="{binding registration}"/> </datatemplate> </gridviewcolumn.celltemplate> </gridviewcolumn> </gridview> </listview.view> </listview> </grid> </window>
code-behind:
namespace gridviewcontextmenu { /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); viewmodel vm = new viewmodel(); this.datacontext = vm; } } }
viewmodel
using system; using system.collections.generic; using system.componentmodel; using system.linq; using system.text; using system.threading.tasks; namespace gridviewcontextmenu { public class viewmodel : inotifypropertychanged { private system.collections.objectmodel.observablecollection<car> mcars; public system.collections.objectmodel.observablecollection<car> cars { { if (mcars == null) { mcars = new system.collections.objectmodel.observablecollection<car>(); } return mcars; } set { mcars = value; onpropertychanged("cars"); } } public viewmodel() { (int = 0; < 5; i++) { car car = new car(); car.company = "company" + i; car.model = "model" + i; car.name = "name" + i; car.registration = "registration" + i; cars.add(car); } } public event propertychangedeventhandler propertychanged; protected virtual void onpropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(propertyname)); } } } }
model class
using system; using system.collections.generic; using system.componentmodel; using system.linq; using system.text; using system.threading.tasks; namespace gridviewcontextmenu { public class car : system.componentmodel.inotifypropertychanged { private system.string mname; public system.string name { { return mname; } set { mname = value; onpropertychanged("name"); } } private system.string mmodel; public system.string model { { return mmodel; } set { mmodel = value; onpropertychanged("model"); } } private system.string mcompany; public system.string company { { return mcompany; } set { mcompany = value; onpropertychanged("company"); } } private system.string mregistration; public system.string registration { { return mregistration; } set { mregistration = value; onpropertychanged("registration"); } } public event propertychangedeventhandler propertychanged; protected virtual void onpropertychanged(string propertyname) { propertychangedeventhandler handler = propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(propertyname)); } } } }
i have written it's easy 1 understand,
when click name column cell it's showing dynamicperiodcontextmenu
when press shift + f10 it's not showing context menu.
when open contextmenu right click wpf knows clicked (on grid in cell template) , opens contextmenu associated grid.
using shift+f10 work (out of box) contextmenu on listview itself.
for example if set focusable = true on grid in template , use tab set focus on grid, can use shift+f10 fine.
<grid contextmenu="{staticresource dynamicperiodcontextmenu}" focusable="true"> <textblock text="{binding name}"/> </grid>
Comments
Post a Comment