c# - PropertyChanged for a custom class is always null -
i have problem inotifypropertychanged
, binding, watched dozen questions here, problem still exists.
my xaml code:
<window x:class="daa.ist.choosepc.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:daa.ist.choosepc" mc:ignorable="d" title="mainwindow" height="350" width="525"> <window.resources> <local:question x:key="expertsystem" text="question"/> </window.resources> <grid datacontext="{staticresource expertsystem}"> <label x:name="description" content="Определение опимальной конфигурации ПК" horizontalalignment="left" margin="100,30,0,0" verticalalignment="top"/> <button x:name="button" content="" horizontalalignment="left" margin="204,167,0,0" verticalalignment="top" width="75" click="button_click"/> <button x:name="button1" content="" horizontalalignment="left" margin="284,167,0,0" verticalalignment="top" width="75" click="button1_click"/> <label x:name="questionlable" content="{binding text}" horizontalalignment="left" margin="100,70,0,0" verticalalignment="top"/> <label x:name="result" content="" horizontalalignment="left" margin="100,210,0,0" verticalalignment="top"/> </grid>
i list here full code, because don't know there problem is, sorry. main window code:
public partial class mainwindow : window { public static iquestion question; public mainwindow() { initializecomponent(); question = new question { text = "question", answers = new list<ianswer> { new answer {text = "answer", nextquestionnumber = 0}, new answer {text = "answer1", nextquestionnumber = 0} } }; datacontext = question; } private void button_click(object sender, routedeventargs e) { question.answer(0); } private void button1_click(object sender, routedeventargs e) { question.answer(1); } }
and question
code, class implements inotifypropertychanged
:
public class question : iquestion, inotifypropertychanged { private string _text; public string text { { return _text; } set { _text = value; onpropertychanged("text"); } } public ilist<ianswer> answers { get; set; } public void answer(int number) { mainwindow.question.answers[number].number = number; mainwindow.question.answers[number].setnextquestion(); } public event propertychangedeventhandler propertychanged; [notifypropertychangedinvocator] protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } }
please me, sure miss or don't know something. if can give me advise changing of ilist<ianswer> answers
, thankful him^^
first of all, bad practice refer mainwindow inside question class. should use commands in context i.e. should define command in datacontext( question here) , bind command 2 button's command property.
for example:
class question : inotifypropertychanged, iquestion { public icommand buttonclickcommand { get; set;} public question() { //bind buttonclickcommand event handler/ method buttonclickcommand = new delegatecommand(eventhandlername); } public void eventhandlername() { } }
also, take private _answers field in class , raise onpropertychanged event setter of answers property too, utilize list in notification mechanism i.e.
public ilist<ianswer> answers { { return _answers } set { _nswers = value; onpropertychanged("answers");
}
and, have mentioned want chnge content of label , buttons. change content of label , buttons, need bind 'content' of these controls properties in question class well. don't see binding in xaml of now.
Comments
Post a Comment