c# - DataGrid CellStyle Setters with Freezable StaticRecource -


i wanted set command of button in wpf datagrid setter. seems dp property commandproperty gets ovewritten default value null after returned copy in createinstancecore(), original command gets lost.

if bind staticresource directly works without problems. there way stop behavior or solution?

public class resourcecommand : freezable, icommand {      public icommand command {         { return (icommand)getvalue(commandproperty); }         set { setvalue(commandproperty, value); }     }      // using dependencyproperty backing store command.  enables animation, styling, binding, etc...     public static readonly dependencyproperty commandproperty =         dependencyproperty.register("command", typeof(icommand), typeof(resourcecommand), new uipropertymetadata(null, commandpropertychangedcallback));       static void commandpropertychangedcallback(dependencyobject d, dependencypropertychangedeventargs e) {         resourcecommand resourcecommand = (resourcecommand)d;         int h = resourcecommand.gethashcode();         if (e.oldvalue != null)             ((icommand)e.oldvalue).canexecutechanged -= resourcecommand.oncanexecutechanged;         if (e.newvalue != null)             ((icommand)e.newvalue).canexecutechanged += resourcecommand.oncanexecutechanged;     }      #region icommand member      public bool canexecute(object parameter) {         if (command == null)             return false;         return command.canexecute(parameter);     }      public event eventhandler canexecutechanged;      void oncanexecutechanged(object sender, eventargs e) {         if (canexecutechanged != null)             canexecutechanged(sender, e);     }      public void execute(object parameter) {         command.execute(parameter);     }      #endregion      protected override freezable createinstancecore() {         resourcecommand resourcecommand = new resourcecommand();         resourcecommand.command = command;         return resourcecommand;     } } 

xaml:

<window.resources>     <local:resourcecommand x:key="firstcommand"  command="{binding firstcommand}" />     <local:resourcecommand x:key="secondcommand"  command="{binding secondcommand}" /> </window.resources> <grid>     <datagrid itemssource="{binding collection}">         <datagrid.columns>             <datagridtemplatecolumn>                 <datagridtemplatecolumn.celltemplate>                     <datatemplate>                         <button content="click me">                             <button.style>                                 <style targettype="button">                                     <setter property="command" value="{staticresource firstcommand}" />                                 </style>                             </button.style></button>                     </datatemplate>                 </datagridtemplatecolumn.celltemplate>             </datagridtemplatecolumn>         </datagrid.columns>     </datagrid> </grid> 

it works if define resource commands this:

<local:resourcecommand x:key="firstcommand" command="{binding firstcommand}" x:shared="false"/> 

using technique can throw not-implemented in createinstancecore , you'll using freezable enable data binding.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -