c# - Setter of a Container ViewModel class property that is dependent on another property of Contained ViewModel class -
please have @ next code
public class vm1 { private bool isvalid; private vm2 vminstance; // ... public bool isvalid { { homecoming this.isvalid; } set { /* ... */ } } public vm2 vminstance { // ... } } public class vm2 { public bool isvalid { // ... } } now isvalid-property of vm1 dependent on value of isvalid-property of vm2. best way accomplish that?
i have found way solve problem. vm1 class subscribes propertychanged event of vm2. , whenevr isvalid property of vm2 changed, in eventhandler, phone call setstate() method of vm1 decide value of isvalid property.
void myviewmodel_propertychanged(object sender, propertychangedeventargs e) { switch (e.propertyname) { case "isvalid": // phone call method in vm1 decide value of isvalid property of vm1 // say, method name setstate() break; } } is right approach?
i'll need more info on how you're going consider vm2's isvalid property.
you can start passing instance of vm2 constructor of vm1 may referenced. @ next example.
public class vm1 { private bool isvalid; private vm2 vminstance; //...... public vm1(vm2 vm2) { vminstance = vm2; } public bool isvalid { { homecoming this.isvalid && vminstance.isvalid; } set { // } } public vm2 vminstance { { homecoming vminstance; } set { vminstance = value; } } } public class vm2 { public bool isvalid { get; set; } } c# wpf mvvm
No comments:
Post a Comment