WPF-MVVM: Handeling Event from within DataGrid Style -
i develop prism+mef application based on wpf+mvvm, there many datagrids build datagridstyle applied datagrids in modules. style add filter text box in columns headers, , used mvvm light eventtocommand trigger textchanged event when text box text changed this: (this code exist in datagridstyle resource dictionary)
<textbox x:name="filtertextbox" horizontalalignment="right" minwidth="25" height="auto" opacitymask="black" visibility="collapsed" text="" textwrapping="wrap" grid.column="0" grid.columnspan="1"> <i:interaction.triggers> <i:eventtrigger eventname="textchanged"> <cmd:eventtocommand command="{binding datacontext.textchangedcommand}" passeventargstocommand="true"/> </i:eventtrigger> </i:interaction.triggers> </textbox>
then handled textchangedcommand in viewmodel (pertaining view contains data grid)using attached property:
#region textchangedcommand---------------------------------------------------------------------------------------------- static icommand command; //1 public static icommand gettextchangedcommand(dependencyobject obj) { return (icommand)obj.getvalue(textchangedcommandproperty); } public static void settextchangedcommand(dependencyobject obj, icommand value) { obj.setvalue(textchangedcommandproperty, value); } // using dependencyproperty backing store textchangedcommand. enables animation, styling, binding, etc... public static readonly dependencyproperty textchangedcommandproperty = dependencyproperty.registerattached("textchangedcommand", typeof(icommand), typeof(subsystemallviewmodel), new uipropertymetadata(null, commandchanged)); static void commandchanged(dependencyobject obj, dependencypropertychangedeventargs e) { var fe = obj frameworkelement; command = e.newvalue icommand; fe.addhandler(textbox.textchangedevent, new textchangedeventhandler(executecommand)); } static void executecommand(object sender, textchangedeventargs e) { //some code } #endregion
and view contains grid:
<datagrid itemssource="{binding subsystems,mode=twoway, updatesourcetrigger=propertychanged}" selecteditem="{binding path=selectedsubsystem, mode=twoway}" name="subsystemalldatagrid" style="{staticresource datagridstyle}" grid.row="2"> <datagrid.columns> <datagridtextcolumn header="serial" binding="{binding path=serial, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="type" binding="{binding path=type, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="system" binding="{binding path=system, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="subsystemno" binding="{binding path=subsystemno, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="description" binding="{binding path=description, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="scope" binding="{binding path=scope, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="area" binding="{binding path=area, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="priority" binding="{binding path=priority, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="dossierlocation" binding="{binding path=dossierlocation, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="parts" binding="{binding path=parts, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="status" binding="{binding path=status, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="statusdate" binding="{binding path=statusdate, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="mcdate" binding="{binding path=mcdate, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="plnmcdate" binding="{binding path=plnmcdate, mode=twoway}"></datagridtextcolumn> <datagridtextcolumn header="remark" binding="{binding path=remark, mode=twoway}"></datagridtextcolumn> </datagrid.columns> </datagrid>
the problems are: when enter filter text in text box in 1 of columns headers of data grid nothing happen , break points @ following points not hit:
1-gettextchangedcommand , settextchangedcommand
2-the commandchanged() method.
i'm new wpf i'm sure there faults in wpf or c# code..., please me fixing these faults.
note: don't use code behind.
thanks in advance
looks command-binding doesnt work. try following:
command="{binding path=textchangedcommand}"
or
command="{binding relativesource={relativesource ancestortype={x:type datagrid}} path=datacontext.textchangedcommand}"
Comments
Post a Comment