c# - Adding a context menu only to the Expander header -


i'm trying compose data grid (in mvvm 4.5 project) show data grouped specific property (by use of expanders). works i'd add context menu options "expand all" , "collapse all" collapse/expand groups. event handlers menu item click events being handled in window's code-behind.

the problem context menu applied expander , inherited children includes <itemspresenter/> , rows/cells.

i want only apply context menu grouped header itself. achievable if it's applied innards (like stackpanel in example below) in case, context menu isn't accessible entire header line, on stackpanel contents/text.

i'm planning on using different context menu items (add/edit etc) , have collapse/expand context menu apply group header. achievable?

        <datagrid name="dgdata" itemssource="{binding myitems}" autogeneratecolumns="false" canuseraddrows="false" canuserdeleterows="false">              <datagrid.resources>                  <!--groupheader text-->                 <style x:key="gridgrouptextstyle" targettype="textblock">                     <setter property="fontsize" value="12"/>                     <setter property="fontweight" value="bold"/>                 </style>                  <!--groupheader contextmenu-->                 <contextmenu x:key="cm_columnheadermenu">                     <menuitem name="mi_expandall" header="expand groups"/>                     <menuitem name="mi_collapseall" header="collapse groups"/>                 </contextmenu>                  <!--grouping style-->                 <style x:key="filesgroupheaderstyle" targettype="{x:type groupitem}">                     <setter property="template">                         <setter.value>                             <controltemplate targettype="{x:type groupitem}">                                 <expander x:name="exp" isexpanded="true">                                     <expander.style>                                         <style targettype="{x:type expander}">                                             <setter property="contextmenu" value="{staticresource cm_columnheadermenu}"/>                                         </style>                                     </expander.style>                                     <expander.header>                                         <stackpanel orientation="horizontal">                                             <textblock text="{binding name}" style="{staticresource gridgrouptextstyle}"/>                                             <textblock text=" (" style="{staticresource gridgrouptextstyle}"/>                                             <textblock text="{binding itemcount}" style="{staticresource gridgrouptextstyle}"/>                                             <textblock text=")" style="{staticresource gridgrouptextstyle}"/>                                         </stackpanel>                                     </expander.header>                                     <itemspresenter/>                                 </expander>                             </controltemplate>                         </setter.value>                     </setter>                 </style>             </datagrid.resources>              <datagrid.groupstyle>                 <groupstyle containerstyle="{staticresource filesgroupheaderstyle}">                     <groupstyle.panel>                         <itemspaneltemplate>                             <datagridrowspresenter/>                         </itemspaneltemplate>                     </groupstyle.panel>                 </groupstyle>             </datagrid.groupstyle>              <datagrid.columns>                 <datagridtextcolumn header="column 1" binding="{binding prop1}" isreadonly="true"/>                 <datagridtextcolumn header="column 2" binding="{binding prop2}" isreadonly="true"/>                 <datagridtextcolumn header="column 3" binding="{binding prop3}" isreadonly="true"/>             </datagrid.columns>                          </datagrid> 

if understand right, want click anywhere in stackpanel , contextmenu work this:

contextmenu

if yes, here go in code:

 <expander grid.row="1">             <expander.style>                 <style targettype="{x:type expander}">                     <setter property="contextmenu" value="{x:null}"/>                 </style>             </expander.style>             <expander.header>                 <stackpanel orientation="horizontal" width="{binding path=actualwidth, relativesource={relativesource findancestor, ancestortype={x:type expander}}}" background="pink" horizontalalignment="stretch">                     <stackpanel.style>                         <style targettype="stackpanel" >                             <setter property="contextmenu"  value="{staticresource cm_columnheadermenu}"></setter>                         </style>                     </stackpanel.style>                     <textblock text="hello" />                     <textblock text=" (" />                     <textblock text="world" />                     <textblock text=")" />                     <textblock text="{binding path=actualwidth, relativesource={relativesource findancestor, ancestortype={x:type expander}}}" />                 </stackpanel>             </expander.header>             <textblock text="dummy"></textblock>         </expander> 

what did do?

  • first, bind stackpanel-width actual width of expander
  • second, make stackpanel stretch.

hope helps


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -