Right click and copy content menu on c# WPF datagrid -


so have wpf data grid 8 cells in each row, i've included relevant 1 simplicity i'd user able right click cell , copy contents windows clipboard without left clicking , selecting first. i've tried many code snippets can't seem work. each row binded item.

the majority of things i've been trying mouserightbuttondown event. have tried xy position, have used e.originalsource frameworkelement can't seem work. not sure if because datagridhyperlinkcolumn opposed other types used in examples?

i'm c# n00b! appreciated.

    <datagrid x:name="eventsdatagrid" autogeneratecolumns="false" isreadonly="true" itemssource="{binding}" horizontalalignment="left" margin="10,143,0,0" verticalalignment="top" height="295"  canuseraddrows="false" canuserreordercolumns="false" canuserresizecolumns="true" canusersortcolumns="false" borderthickness="1" horizontalscrollbarvisibility="disabled"  fontsize="10" width="1003" mouserightbuttondown="eventsdatagrid_mouserightbuttondown">          <datagrid.contextmenu>             <contextmenu>                 <menuitem header="copy url" click="copyurl">                 </menuitem>             </contextmenu>         </datagrid.contextmenu>          <datagrid.columns>              <datagridhyperlinkcolumn width="230" header="url" binding="{binding url}"  canuserresize="false">                 <datagridhyperlinkcolumn.headerstyle>                     <style targettype="{x:type datagridcolumnheader}">                         <setter property="tooltip" value="url of website" />                         <setter property="horizontalcontentalignment" value="center"/>                         <setter property="verticalalignment" value="center"/>                     </style>                 </datagridhyperlinkcolumn.headerstyle>                  <datagridhyperlinkcolumn.cellstyle>                     <style targettype="{x:type datagridcell}">                         <setter property="foreground" value="black" />                         <setter property="horizontalalignment" value="center"/>                         <setter property="fontsize" value="12"/>                     </style>                 </datagridhyperlinkcolumn.cellstyle>             </datagridhyperlinkcolumn>          </datagrid.columns>     </datagrid> 

the following example shows how use single context menu resource multiple target elements. note might idea create custom command instead of 'borrowing' applicationcommands.copy purpose of demonstration, did here.

<window x:class="wpfapplication2.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="mainwindow" height="350" width="525"         loaded="window_loaded">     <window.commandbindings>         <commandbinding command="applicationcommands.copy"                         executed="copycommand_executed"                         canexecute="copycommand_canexecute"/>     </window.commandbindings>     <window.resources>         <contextmenu x:key="ctmenu" datacontext="{binding placementtarget,relativesource={relativesource self}}">             <menuitem header="copy url"                       command="applicationcommands.copy"                       commandtarget="{binding}"                       commandparameter="{binding text}"/>         </contextmenu>     </window.resources>     <stackpanel>         <textblock text="123" contextmenu="{staticresource ctmenu}"/>         <textblock text="456" contextmenu="{staticresource ctmenu}"/>     </stackpanel> </window> 

the command binding needs code behind (will different custom command implementation)

private void copycommand_executed(object sender, executedroutedeventargs e) {     clipboard.settext(e.parameter string); }  private void copycommand_canexecute(object sender, canexecuteroutedeventargs e) {     if (!string.isnullorempty(e.parameter string))     {         e.canexecute = true;         e.handled = true;     } } 

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 -

android - CoordinatorLayout, FAB and container layout conflict -