c# - Multiple ListViews sharing a ContextMenu, how can I reference the right object? -
i have many listviews, each bound own listcollectionview, each identical contextmenu needs. don't want repeat same contextmenu n times, define in resources , refer via staticresource.
when item x in listview right clicked, , menuitem clicked, how can access object x in codebehind?
<window.resources> <contextmenu x:key="commoncontextmenu"> <menuitem header="do stuff" click="dostuff_click" /> </contextmenu> </window.resources> <listview itemssource="{binding path=listcollectionview1}" contextmenu="{staticresource commoncontextmenu}"> ... </listview> <listview itemssource="{binding path=listcollectionview2}" contextmenu="{staticresource commoncontextmenu}"> ... </listview> private void dostuff_click(object sender, routedeventargs e) { // how selected item of right listview? }
update
thanks michael gunter's answer, using following extension methods:
public static listview getlistview(this menuitem menuitem) { if (menuitem == null) return null; var contextmenu = menuitem.parent contextmenu; if (contextmenu == null) return null; var listviewitem = contextmenu.placementtarget listviewitem; if (listviewitem == null) return null; return listviewitem.getlistview(); } public static listview getlistview(this listviewitem item) { (dependencyobject = item; != null; = visualtreehelper.getparent(i)) { var listview = listview; if (listview != null) return listview; } return null; }
1) put context menu on each item within each listview
, rather on each listview
itself. avoids context menu popping when clicking on empty space in listview
. this, use listview.itemcontainerstyle
property. (if want context menu on listview
itself, let me know , i'll edit answer accordingly.)
<window.resources> <contextmenu x:key="commoncontextmenu"> <menuitem header="do stuff" click="dostuff_click" /> </contextmenu> <style x:key="listviewitemstyle" targettype="{x:type listviewitem}"> <setter property="contextmenu" value="{staticresource commoncontextmenu}" /> </style> </window.resources> <listview itemssource="{binding path=listcollectionview1}" itemcontainerstyle="{staticresource listviewitemstyle}"> ... </listview> <listview itemssource="{binding path=listcollectionview2}" itemcontainerstyle="{staticresource listviewitemstyle}"> ... </listview>
2) use code following determine item right-clicked upon.
private void dostuff_click(object sender, routedeventargs e) { var menuitem = sender menuitem; if (menuitem == null) return; var contextmenu = menuitem.parent contextmenu; if (contextmenu == null) return; var listviewitem = contextmenu.placementtarget listviewitem; if (listviewitem == null) return; var listview = getlistview(listviewitem); if (listview == null) return; // stuff here } private listview getlistview(listviewitem item) { (dependencyobject = item; != null; = visualtreehelper.getparent(i)) { var listview = listview; if (listview != null) return listview; } return null; }
Comments
Post a Comment