c# - In WPF, a property should be changed by its name or by use of binding -
i new wpf , question arose in mind while developing app. assume have textbox defined below
<textbox x:name="mytextbox" />
then in c# code, can change string shown in textbox using following command
mytextbox.text = "hello!";
however, there way have same behavior use of binding when in xaml have
<textbox x:name="mytextbox" text="{binding content}" />
and in c# have
public class mytext : inotifypropertychanged { public event propertychangedeventhandler propertychanged; private string _content; public string content { { return _content; } set { _content = value; propertychanged(this, new propertychangedeventargs("content")); } } } mytext txt = new mytext(); mytextbox.datacontext = txt; txt.content = "hello!";
obviously second option needs more coding, result of both of them same. however, in second case not have care executing code on ui thread. everywhere in code when change txt.content
string in textbox change without exception.
my question is: there design issue preferences on of these 2 options changing property?
the second option prerequisite employing mvvm pattern. in first option there no decoupling; every operation takes place in view. if prefer section option , apply mvvm pattern, have 2 different classes; 1 implementing ui - namely view, , 1 abstracting view , model.
you may refer this web page more detail.
Comments
Post a Comment