Validating two usercontrols

public class Product Model : Observable Object { #region Fields private int _product Id; private string _product Name; private decimal _unit Price; #endregion // Fields #region Properties public int Product Id { get { return _product Id; } set { if (value !

= _product Id) { _product Id = value; On Property Changed("Product Id"); } } } public string Product Name { get { return _product Name; } set { if (value !

= _product Name) { _product Name = value; On Property Changed("Product Name"); } } } public decimal Unit Price { get { return _unit Price; } set { if (value !

validating two usercontrols-80

I would actually recommend looking into Microsoft PRISM’s Notification Object or MVVM Light’s View Model Base which does the same thing once you are comfortable with MVVM, but for now I wanted to keep 3rd party libraries out of this and to show the code. = null) { var e = new Property Changed Event Args(property Name); this.

protected virtual void On Property Changed(string property Name) { this. Property Changed(this, e); } } #endregion // INotify Property Changed Members #region Debugging Aides /// [Conditional("DEBUG")] [Debugger Step Through] public virtual void Verify Property Name(string property Name) { // Verify that the property name matches a real, // public, instance property on this object. Get Properties(this)[property Name] == null) { string msg = "Invalid property name: " + property Name; if (this.

Throw On Invalid Property Name) throw new Exception(msg); else Debug.

In my opinion, if you are using WPF or Silverlight you should be using the MVVM design pattern.

It is perfectly suited to the technology and allows you to keep your code clean and easy to maintain. They should only contain properties and property validation.

The problem is, there are a lot of online resources for MVVM, each with their own way of implementing the design pattern and it can be overwhelming. They are not responsible for getting data, saving data, click events, complex calculations, business rules, or any of that stuff. In most cases, they can be Data Templates which is simply a template that tells the application how to display a class.I would like to present MVVM in the simplest way possible using just the basics. It is OK to put code behind your view IF that code is related to the View only, such as setting focus or running animations. This is where the majority of your code-behind goes: data access, click events, complex calculations, business rules validation, etc. For example, if a View contains a List Box of objects, a Selected object, and a Save button, the View Model will have an Observable Collection Obect List, Model Selected Object, and ICommand Save Command.I’ve put together a small sample showing these 3 layers and how they relate to each other.You’ll notice that other than property/method names, none of the objects need to know anything about the others.Once the interfaces have been designed, each layer can be built completely independent of the others. You’ll notice that the only thing this class contains is properties and change notification code.Usually I would also implement IData Error Info here for property validation, however I have left this out for now.

Tags: , ,