c# - Pass type parameters to attached behaviors -


i implementing attached behavior in wpf application. need pass type parameters behavior, can call method void newrow(table<t> table) on sqliteboundrow. if instantiating object in xaml, pass type parameters using x:typearguments, don't see way when setting attached behavior, because uses static property.

code attached behavior looks this:

public abstract class sqliteboundrow<t> t : sqliteboundrow<t> {     public abstract void newrow(table<t> table); }  public class datagridbehavior<t> t:sqliteboundrow<t> {     public static readonly dependencyproperty isenabledproperty;     static datagridbehavior()     {         isenabledproperty = dependencyproperty.registerattached("isenabled",             typeof(bool), typeof(datagridbehavior<t>),             new frameworkpropertymetadata(false, onbehaviorenabled));     }     public static void setisenabled(dependencyobject obj, bool value)     {         obj.setvalue(isenabledproperty, value);     }     public static bool getisenabled(dependencyobject obj)     {         return (bool)obj.getvalue(isenabledproperty);     }     private static void onbehaviorenabled(dependencyobject dependencyobject,         dependencypropertychangedeventargs args)     {         var dg = dependencyobject datagrid;         dg.initializingnewitem += datagrid_initializingnewitem;     }     private static void datagrid_initializingnewitem(object sender,         initializingnewitemeventargs e)     {         var table = (sender datagrid).itemssource table<t>;         (e.newitem t).newrow(table);     }  } 

xaml looks this:

<datagrid datagridbehavior.isenabled="true">     <!-- datagridbehavior needs type parameter --> </datagrid> 

my current solution wrap datagridbehavior in a derived class specifies type parameters.

the simplest solution declare attached property, of type type hold parameter value you. in case, set type property before isenabled attached property:

<datagrid datagridbehavior.typeparameter="{x:type someprefix:sometype}"      datagridbehavior.isenabled="true" ... /> 

looking again @ code, seems though isenabled property nothing except adding new row table... in case, there's no reason why couldn't replace typeparameter attached property , use 1 add new row instead.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -