c# - Bind function to button in ListView using delegate in model -
i'm new c# , i'm trying bind function in each item of collection button item. collection list<assessmentitem>, each assessmentitem following:
public class assessmentitem { public string label { get; set; } public string explanation { get; set; } public string resourceurl { get; set; } public bitmapimage icon { get; set; } public runfixer fixer { get; set; } } public runfixer fixer delegate want bind button particular assessmentitem. here datatemplate i'm using accomplish plans:
<datatemplate x:key="assessmentlisttemplate"> <grid margin="0,10"> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="*"/> <columndefinition width="auto"/> </grid.columndefinitions> <textblock text="{binding label}" fontsize="16" verticalalignment="center" grid.column="0" fontweight="bold" margin="15,0,0,0"/> <button grid.column="2" margin="0,0,10,0"> <image source="{binding icon}" width="64" height="64"/> </button> </grid> </datatemplate> how bind <button>'s click handler runfixer delegate? tried {binding fixer}, didn't work. changed fixer mousebuttoneventhandler, didn't work either.
thank time looking @ this! don't mind being educated.
additions
the runfixer delegate declared with
public delegate void runfixer();
final code
for personal documentation , other's satisfaction, i'm posting result worked me:
public class assessmentitem { public string label { get; set; } public string explanation { get; set; } public string resourceurl { get; set; } public bitmapimage icon { get; set; } public runfixer fixer { get; set; } delegatecommand _fixercommand = null; public icommand fixercommand { { if (_fixercommand == null) { _fixercommand = new delegatecommand(() => fixer()); } return _fixercommand; } } } and in datatemplate:
<datatemplate x:key="assessmentlisttemplate"> <grid margin="0,10"> <grid.columndefinitions> <columndefinition width="auto"/> <columndefinition width="*"/> <columndefinition width="auto"/> </grid.columndefinitions> <textblock text="{binding label}" fontsize="16" verticalalignment="center" grid.column="0" fontweight="bold" margin="15,0,0,0"/> <button grid.column="2" margin="0,0,10,0" command="{binding fixercommand}"> <image source="{binding icon}" width="64" height="64"/> </button> </grid> </datatemplate> hope helps!
you can't bind function button. can bind command object button's command property. in order need modify class:
public class assessmentitem { public string label { get; set; } public string explanation { get; set; } public string resourceurl { get; set; } public bitmapimage icon { get; set; } public runfixer fixer { get; set; } icommand _fixercommand = new uicommand(); public icommand fixercommand { { _fixercommand = _fixercommand ?? new delegatecommand<object>((o)=> {var f = fixer; if(f != null) f();}); return _fixercommand;} } } i'm using delegatecommand class part of prism library can downloaded here
then you're modifying data template bind fixercommand property
<button grid.column="2" margin="0,0,10,0" command ="{binding fixercommand}"> <image source="{binding icon}" width="64" height="64"/> </button>
Comments
Post a Comment