c# - WPF UserControl Binding Problem -


i create usercontrol own header property.

public partial class someclass: usercontrol, inotifypropertychanged {     public someclass()     {         initializecomponent();     }      private string header;     public string header     {         { return header; }         set          {              header = value;             onpropertychanged("header");         }     }      protected void onpropertychanged(string propertyname)                         {                                                                                 if (this.propertychanged != null)                                                 propertychanged(this, new propertychangedeventargs(propertyname));     }      public event propertychangedeventhandler propertychanged; } 

in usercontol xaml:

label name="lbheader" grid.column="0" content="{binding path=header}" 

if set value: aa2p.header = "someheeadertext"; label.caption not changed. how can solve problem?

in windows xaml:

uc:someclass x:name="aa2p"  

if give directly value label (lbheader.content = header;) instead of onpropertychanged("header"); work but, why not work onpropertychanged?


i need use datacontext somethig else. try use dependency property wrong.

public partial class tester : usercontrol {     public tester()     {         initializecomponent();     }      public string header     {         { return (string)getvalue(mydependencyproperty); }         set { setvalue(mydependencyproperty, value); }     }     public static readonly dependencyproperty mydependencyproperty =     dependencyproperty.register("mydependencyproperty", typeof(string), typeof(string));  }   <usercontrol ... x:name="maincontrol"> <textblock text="{binding elementname=maincontrol, path=mydependencyproperty}"/> </usercontrol>   <window ...> <my:tester header="sometext" /> </window> 

it not work. wrong? thanks!

the easiest approach datacontext of object. 1 way of doing directly in constructor this:

public someclass()     {         initializecomponent();         datacontext = this;     } 

setting datacontext specify new data should fetched from. there great tips , information in article called wpf basic data binding faq. read better understand datacontex can used for. essential component in wpf/c#.


update due update of question.

to understanding should change first argument of dependencyproperty.register name of property want bind to, here "header" second argument type of class, here someclass. leave with:

public static readonly dependencyproperty mydependencyproperty =     dependencyproperty.register("header", typeof(someclass), typeof(string)); 

but seldom use dependency properties not positive it, worth try..


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -