Binding a textBlock text to a value in a custom class xaml c# -
when navigate new page should display text, appears empty
the xaml code have
xmlns:vm="using:estimation" <page.datacontext> <vm:playerclass/> </page.datacontext>
this textblock im trying bind data too.
<textblock x:name="playerone" text="{binding playeronename}" />
the class im binding follows
public class playerclass :inotifypropertychanged { public event propertychangedeventhandler propertychanged; private void notifypropertchanged(string info) { if (propertychanged != null) propertychanged(this, new propertychangedeventargs(info)); } private string name; public string playeronename { { return this.name; } set { this.name = value ; notifypropertchanged(playeronename); } } }}
and class im changing content in text box is
private void startbutton_click(object sender, routedeventargs e) { if (playeronetextbox.text == enternametext || playertwotextbox.text == enternametext || playerthreetextbox.text == enternametext || playerfourtextbox.text == enternametext) { messagedialog msgbox = new messagedialog("please enter names before continuing"); msgbox.showasync(); } else { // playernames.propertychanged += new dependencypropertychangedeventhandler(playernames_propertychanged); this.datacontex.playeronename = playeronetextbox.text; messagedialog msgbox = new messagedialog(playernames.playeronename); msgbox.showasync(); playernames.playertwoname = playertwotextbox.text; playernames.playerthreename = playertwotextbox.text; playernames.playerfourname = playerfourtextbox.text; frame.navigate(typeof(newroundpage)); } }
in constructor set name
public playerclass () { playeronename = "jabba de hutt"; }
also set fallback value provide indicator of failed binding situation:
text="{binding playeronename, fallback=unknown}"
the navigate should not change datacontext of textbox, change viewmodel instead
protected override void onnavigatedto(navigationeventargs e) { var playernames = e.parameter playerclass; this.datacontext.playeronename = playernames.playeronename; }
Comments
Post a Comment