TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
mopuru surya
NA
6
559
Validation of texbox wpf mvvm
Sep 13 2019 12:36 PM
public class ProductModel : NotifyPropertyChangeBase , IDataErrorInfo 2: { 3: public bool IsValidating = false; 4: 5: public Dictionary
Errors = new Dictionary
(); 6: 7: ...properties... 8: 9: public bool IsValid() 10: { 11: IsValidating = true; 12: try 13: { 14: NotifyPropertyChanged(() => Name); 15: NotifyPropertyChanged(() => Price); 16: NotifyPropertyChanged(() => Amount); 17: } 18: finally 19: { 20: IsValidating = false; 21: } 22: return (Errors.Count() == 0); 23: } 24: 25: public string Error 26: { 27: get { throw new NotImplementedException(); } 28: } 29: 30: public string this[string columnName] 31: { 32: get 33: { 34: string result = string.Empty; 35: if (!IsValidating) return result; 36: Errors.Remove(columnName); 37: switch (columnName) 38: { 39: case "Name": if (string.IsNullOrEmpty(Name)) result = "Name is required!"; break; 40: case "Price": if ((Price < 10) || (Price > 1000)) result = "Price must be between 10 and 1000"; break; 41: case "Amount": if ((Amount < 1) || (Amount > 100)) result = "Amount must be between 1 and 100"; break; 42: }; 43: if (result != string.Empty) Errors.Add(columnName, result); 44: return result; 45: } 46: } 47: 48: public ProductModel() 49: { 50: } 51: } private void SaveExecute() 2: { 3: Model.IsValid(); 4: } UserControl x:Class="WPFValidationApplication.Views.ProductForm" 2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4: xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5: xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6: xmlns:my="clr-namespace:WPFValidationApplication.Utils" 7: d:DesignHeight="300" 8: d:DesignWidth="448" 9: mc:Ignorable="d"> 10: 11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
29:
35:
41: 42: 43:
51: 52:
60: 61:
69: 70:
79:
80:
81: public class ProductModel : NotifyPropertyChangeBase 2: { 3: private String name; 4: public String Name 5: { 6: get 7: { 8: return name; 9: } 10: set 11: { 12: name = value; 13: NotifyPropertyChanged(() => Name); 14: } 15: } 16: 17: private decimal price; 18: public decimal Price 19: { 20: get 21: { 22: return price; 23: } 24: set 25: { 26: price = value; 27: NotifyPropertyChanged(() => Price); 28: } 29: } 30: 31: private int amount; 32: public int Amount 33: { 34: get 35: { 36: return amount; 37: } 38: set 39: { 40: amount = value; 41: NotifyPropertyChanged(() => Amount); 42: } 43: } 44: } public partial class ProductForm : UserControl 2: { 3: public ProductForm() 4: { 5: InitializeComponent(); 6: this.DataContext = new ProductFormViewModel(); 7: } 8: } public class ProductFormViewModel : NotifyPropertyChangeBase 2: { 3: public ProductModel Model { get; set; } 4: 5: public ICommand SaveCommand { get; set; } 6: 7: public ProductFormViewModel() 8: { 9: Model = new ProductModel(); 10: SaveCommand = new DelegateCommand(SaveExecute); 11: } 12: 13: private void SaveExecute() 14: { 15: //dummy 16: } 17: } public class ProductModel : NotifyPropertyChangeBase 2: { 3: private String name; 4: public String Name 5: { 6: get 7: { 8: return name; 9: } 10: set 11: { 12: name = value; 13: NotifyPropertyChanged(() => Name); 14: } 15: } 16: 17: private decimal price; 18: public decimal Price 19: { 20: get 21: { 22: return price; 23: } 24: set 25: { 26: price = value; 27: NotifyPropertyChanged(() => Price); 28: } 29: } 30: 31: private int amount; 32: public int Amount 33: { 34: get 35: { 36: return amount; 37: } 38: set 39: { 40: amount = value; 41: NotifyPropertyChanged(() => Amount); 42: } 43: } 44: } My form doesnot show up any red border or error message on button click
Reply
Answers (
2
)
Grid can't set ColumnDefinition at X-Axis: 840653000 PX
Uppercase Textbox in WPF