如何在按钮单击时创建mvvm wpf验证 [英] how to create mvvm wpf validation on button click
问题描述
我已经创建了验证,但我想在按钮点击时显示验证..在我的应用程序验证中显示如果textbox为空..我想在按钮中显示消息..
型号
Category.cs
i have created validation but i want to show validation on button click ..in my application validation shown in if textbox is empty..i want show message in button ..
model
Category.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MVVMValidation.CustomValidationAttributes;
using MVVMValidation.Notification;
namespace BillingApplication.Model
{
class Category : IDataErrorInfo, INotifyPropertyChanged
{
private short categoryId;
private string categoryName;
private string categoryCode;
//[Unqiue(ErrorMessage = "Duplicate Id. Id already exists")]
//[Required(ErrorMessage = "Id is Required")]
public short CatogoryId
{
get
{
return CatogoryId;
}
set
{
CatogoryId = value;
}
}
[Required(ErrorMessage = "Name is Required")]
public string CategoryName
{
//get { return GetValue(() => CategoryName); }
//set { SetValue(() => CategoryName, value); }
get
{
return categoryName;
}
set
{
categoryName = value;
NotifyPropertyChanged("CategoryName");
}
}
protected void NotifyPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
// property changed
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
// send app message (mvvm light toolkit)
//if (message != null)
// message(this.IsValid);
}
}
//private void NotifyPropertyChanged(string p)
//{
// throw new NotImplementedException();
//}
//private string GetValue(Func<categoryName> func)
//{
// throw new NotImplementedException();
//}
[Required(ErrorMessage = "cODE is Required")]
public string CategoryCode
{
get
{
return categoryCode;
}
set
{
categoryCode = value;
NotifyPropertyChanged("CategoryCode");
}
}
#region IDataErrorInfo Members
string IDataErrorInfo.Error
{
get
{
return null;
//throw new NotImplementedException();
}
}
string IDataErrorInfo.this[string propertyName]
{
get
{
return GetValidationError(propertyName);
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region Validation
static readonly string[] ValidateProperties = { "CategoryName","CategoryCode" };
public bool IsValid
{
get
{
foreach (string property in ValidateProperties)
//{
if (GetValidationError(property) != null)
return false;
return true;
//}
}
}
string GetValidationError(string propertyName)
{
string error = null;
switch (propertyName)
{
case "CategoryName":
error = ValidateCategoryName();
break;
}
switch (propertyName)
{
case "CategoryCode":
error = ValidateCategoryCode();
break;
}
return error;
}
private string ValidateCategoryCode()
{
if (string.IsNullOrWhiteSpace(CategoryCode))
{
return "Category code Cannot be empty";
}
return null;
}
private string ValidateCategoryName()
{
if (string.IsNullOrWhiteSpace(CategoryName))
{
return "Category Name Cannot be empty";
}
return null;
}
#endregion
}
}
VIewModel
CategoryViewModel.cs
VIewModel
CategoryViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Xml.Serialization;
using BillingApplication.Model;
using GalaSoft.MvvmLight;
using MVVMValidation.Commands;
using MVVMValidation.Notification;
namespace BillingApplication.ViewModel
{
/// <summary>
/// This class contains properties that a View can data bind to.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
class CategoryViewModel : INotifyPropertyChanged
{
public CategoryViewModel()
{
Category = new Category
{
CategoryName="",
CategoryCode=""
};
}
public Category Category
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
using System.Xml.Serialization;
using BillingApplication.Model;
using GalaSoft.MvvmLight;
using MVVMValidation.Commands;
using MVVMValidation.Notification;
namespace BillingApplication.ViewModel
{
/// <summary>
/// This class contains properties that a View can data bind to.
/// <para>
/// See http://www.galasoft.ch/mvvm
/// </para>
/// </summary>
class CategoryViewModel : INotifyPropertyChanged
{
public CategoryViewModel()
{
Category = new Category
{
CategoryName="",
CategoryCode=""
};
}
public Category Category
{
get;
set;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
Category.xaml
< window x:class =BillingApplication。 Views.Categoryxmlns:x =#unknown>
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:ignore =http://www.ignore .com
xmlns:model =clr-namespace:BillingApplication.Model
xmlns:views =clr-namespace:BillingApplication.Views
mc:Ignorable =d
xmlns:local =clr-namespace:BillingApplication.ViewModel
Title =类别宽度=867.164高度=532.836>
< window.re来源>
<! -
< window.resources> - >
< local:categoryviewmodel x:key =DataContextxmlns:local =#unknown>
< grid datacontext ={StaticResource DataContext}margin =10>
< textbox name = CategoryNamevalidation.errortemplate ={x:Null}>
Text ={Binding Category.CategoryName,ValidatesOnDataErrors = True,UpdateSourceTrigger = PropertyChanged}
HorizontalAlignment =LeftHeight =75Margin =53,36,0,0TextWrapping =WrapVerticalAlignment =TopWidth =259/>
< Label Content ={Binding ElementName = CategoryName,Path =(Validation.Errors).CurrentItem.ErrorContent}FontFamily =calibriForeground =RedHorizontalAlignment =LeftMargin =400,63,0,0VerticalAlignment = 顶部/>
<标签内容={Binding ElementName = CategoryCode,Path =(Validation.Errors).CurrentItem.ErrorContent}FontFamily =calibriForeground =RedHorizontalAlignment =LeftMargin =400,168,0,0VerticalAlignment =Top RenderTransformOrigin =7.533,0.823/>
< textbox name =CategoryCodevalidation.errortemplate ={x:Null}>
Text = {Binding Category.CategoryCode,ValidatesOnDataErrors = True,UpdateSourceTrigger = PropertyChanged}HorizontalAlignment =LeftHeight =39Margin =53,168,0,0TextWrapping =WrapVerticalAlignment =TopWidth =259/ >
<! - < Label Content ={Binding ElementName = CategoryName,Path =(Validation.Errors).CurrentItem.ErrorContent}/> ; - >
我想要验证代码单击按钮工作..请帮助我
Category.xaml
<window x:class="BillingApplication.Views.Category" xmlns:x="#unknown">
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:ignore="http://www.ignore.com"
xmlns:model="clr-namespace:BillingApplication.Model"
xmlns:views="clr-namespace:BillingApplication.Views"
mc:Ignorable="d"
xmlns:local="clr-namespace:BillingApplication.ViewModel"
Title="Category" Width="867.164" Height="532.836">
<window.resources>
<!--
<window.resources>-->
<local:categoryviewmodel x:key="DataContext" xmlns:local="#unknown">
<grid datacontext="{StaticResource DataContext}" margin="10">
<textbox name="CategoryName" validation.errortemplate="{x:Null}">
Text="{Binding Category.CategoryName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Left" Height="75" Margin="53,36,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="259"/>
<Label Content="{Binding ElementName=CategoryName,Path=(Validation.Errors).CurrentItem.ErrorContent}" FontFamily="calibri" Foreground="Red" HorizontalAlignment="Left" Margin="400,63,0,0" VerticalAlignment="Top"/>
<Label Content="{Binding ElementName=CategoryCode,Path=(Validation.Errors).CurrentItem.ErrorContent}" FontFamily="calibri" Foreground="Red" HorizontalAlignment="Left" Margin="400,168,0,0" VerticalAlignment="Top" RenderTransformOrigin="7.533,0.823"/>
<textbox name="CategoryCode" validation.errortemplate="{x:Null}">
Text="{Binding Category.CategoryCode, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Height="39" Margin="53,168,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="259"/>
<!--<Label Content="{Binding ElementName=CategoryName,Path=(Validation.Errors).CurrentItem.ErrorContent}"/>-->
I want code for validation work in button click..pls help me
推荐答案
尝试
Windows Presentation Foundation中的验证 [ ^ ]
WPF验证 [ ^ ]
WPF中的数据验证 [ ^ ]
Try
Validation in Windows Presentation Foundation[^]
WPF Validation[^]
Data validation in WPF[^]
这篇关于如何在按钮单击时创建mvvm wpf验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!