c# - Bind WPF user control property inside code -
i have mainwindow.xaml
has user control , togglebutton
:
<togglebutton x:name="toggle" content="wait" />
this button sets busydecorator
user control property called isbusyindicatorshowing
, works expected, whenever user clicks on toggle button sets user control property:
<window x:class="mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ctrls="clr-namespace:controls" title="busy" height="300" width="300"> <grid> <grid.rowdefinitions> <rowdefinition height="322*" /> <rowdefinition height="53*" /> </grid.rowdefinitions> <togglebutton x:name="toggle" content="show" margin="228,12,255,397" /> <ctrls:busydecorator horizontalalignment="center" verticalalignment="center" isbusyindicatorshowing="{binding ischecked, elementname=toggle}"> <image name="canvas" stretch="fill" margin="5" /> </ctrls:busydecorator> </grid> </window>
i want bind busydecorator’s isbusyindicatorshowing
property in code. added isbusyindicatorshowing="{binding isbusyindicatorshowing}"
inside user control in xaml like
<ctrls:busydecorator horizontalalignment="center" verticalalignment="center" x:name="actions" isbusyindicatorshowing="{binding isbusyindicatorshowing}"> ...
but not know hot define , set property inside code like
public bool dosomething() { //init //toggle user control busydecorator.isbusyindicatorshowing = true; //do stuff //toggle user control busydecorator.isbusyindicatorshowing = false; return true; }
it not work because says
error 2 object reference required non-static field, method, or property 'controls.busydecorator.isbusyindicatorshowing.get'
the error message key problem, assuming understand question correctly. when "busydecorator.isbusyindicatorshowing = true"
using busydecorator class definition (as though static), not instance have defined in xaml.
you should able name xaml instance (note x:name):
<ctrls:busydecorator x:name="mybusydecorator" horizontalalignment="center" verticalalignment="center" isbusyindicatorshowing="{binding ischecked, elementname=toggle}"> <image name="canvas" stretch="fill" margin="5" /> </ctrls:busydecorator>
then should able refer instance in code , access property in whatever event desire such:
mybusydecorator.isbusyindicatorshowing = true;
Comments
Post a Comment