WPF IDataErrorInfo使用-数据对象上验证
<Window x:Class="DataBindingExam.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="344.636" Width="365.422">
<Grid Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Row="0" Grid.ColumnSpan="2" Grid.Column="0" Margin="3" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Margin="3">ID:</TextBlock>
<TextBox Name="txtID" Margin="3" MinWidth="100"></TextBox>
<Button Name="btnQuery" Margin="3" MinWidth="50" Click="btnQuery_Click_1">查询</Button>
<Button Name="btnUpdate" Margin="3" MinWidth="50" Click="btnUpdate_Click_1">更新</Button>
<Button Name="btnInsert" Margin="3" MinWidth="50" Click="btnInsert_Click_1">插入</Button>
<Button Name="btnDelete" Margin="3" MinWidth="50" Click="btnDelete_Click_1">删除</Button>
</StackPanel>
<TextBlock Margin="3" Grid.Row="1" Grid.Column="0" VerticalAlignment="Center">CategoryID</TextBlock>
<TextBox Name="txtCategoryID" Margin="3" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center"
Text="{Binding Path=CategoryID}"></TextBox>
<TextBlock Margin="3" Grid.Row="2" Grid.Column="0" VerticalAlignment="Center">ModelNumber</TextBlock>
<TextBox Name="txtModelNumber" Margin="3" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center"
Text="{Binding Path=ModelNumber}"></TextBox>
<TextBlock Margin="3" Grid.Row="3" Grid.Column="0" VerticalAlignment="Center">ModelName</TextBlock>
<TextBox Name="txtModelName" Margin="3" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center"
Text="{Binding Path=ModelName}"></TextBox>
<TextBlock Margin="3" Grid.Row="4" Grid.Column="0" VerticalAlignment="Center">ProductImage</TextBlock>
<TextBox Name="txtProductImage" Margin="4" Grid.Row="4" Grid.Column="1" VerticalAlignment="Center"
Text="{Binding Path=ProductImage}"></TextBox>
<TextBlock Margin="3" Grid.Row="5" Grid.Column="0" VerticalAlignment="Center">UnitCost</TextBlock>
<TextBox Name="txtUnitCost" Margin="3" Grid.Row="5" Grid.Column="1" VerticalAlignment="Center" Validation.Error="txtUnitCost_Error_1">
<TextBox.Text>
<Binding Path="UnitCost" NotifyOnValidationError="true">
<Binding.ValidationRules>
<DataErrorValidationRule></DataErrorValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox Name="txtDescription" Margin="4" Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="2"
Text="{Binding Path=Description}" TextWrapping="Wrap"></TextBox>
</Grid>
</Window>
using ClassLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace DataBindingExam
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnQuery_Click_1(object sender, RoutedEventArgs e)
{
int id = Convert.ToInt32(txtID.Text);
Product p = StoreDB.GetProductByID(id);
grid.DataContext = p;
}
private void btnUpdate_Click_1(object sender, RoutedEventArgs e)
{
int categoryID=Convert.ToInt32(txtCategoryID.Text);
decimal unitCost = Convert.ToDecimal(txtUnitCost.Text);
Product p = new Product()
{
CategoryID = categoryID,
ModelNumber = txtModelNumber.Text,
ModelName = txtModelName.Text,
ProductImage = txtProductImage.Text,
UnitCost = unitCost,
Description = txtDescription.Text
};
int productID = Convert.ToInt32(txtID.Text);
StoreDB.UpdateProductByID(productID, p);
}
private void btnInsert_Click_1(object sender, RoutedEventArgs e)
{
int categoryID = Convert.ToInt32(txtCategoryID.Text);
decimal unitCost = Convert.ToDecimal(txtUnitCost.Text);
Product p = new Product()
{
CategoryID = categoryID,
ModelNumber = txtModelNumber.Text,
ModelName = txtModelName.Text,
ProductImage = txtProductImage.Text,
UnitCost = unitCost,
Description = txtDescription.Text
};
StoreDB.InsertProduct(p);
}
private void btnDelete_Click_1(object sender, RoutedEventArgs e)
{
int productID = Convert.ToInt32(txtID.Text);
StoreDB.DeleteProductByID(productID);
}
private void txtUnitCost_Error_1(object sender, ValidationErrorEventArgs e)
{
if (e.Action==ValidationErrorEventAction.Added)
{
MessageBox.Show(e.Error.ErrorContent.ToString());
}
}
}
}
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public class StoreDB
{
public static string connString = Properties.Settings.Default.ConnectionString;
public static Product GetProductByID(int id)
{
Product p = null;
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("GetProductByID", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProductID", id);
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
p = new Product()
{
CategoryID = (int)reader[1],
ModelNumber = reader[2].ToString(),
ModelName = reader[3].ToString(),
ProductImage=reader[4].ToString(),
UnitCost = (decimal)reader[5],
Description = reader[6].ToString()
};
}
return p;
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
public static void UpdateProductByID(int ProductID,Product p)
{
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("UpdateProductByID", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProductID",ProductID);
cmd.Parameters.AddWithValue("@CategoryID",p.CategoryID);
cmd.Parameters.AddWithValue("@ModelNumber",p.ModelNumber);
cmd.Parameters.AddWithValue("@ModelName",p.ModelName);
cmd.Parameters.AddWithValue("@ProductImage",p.ProductImage);
cmd.Parameters.AddWithValue("@UnitCost",p.UnitCost);
cmd.Parameters.AddWithValue("@Description",p.Description);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
public static void InsertProduct(Product p)
{
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("InsertProduct", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@CategoryID", p.CategoryID);
cmd.Parameters.AddWithValue("@ModelNumber", p.ModelNumber);
cmd.Parameters.AddWithValue("@ModelName", p.ModelName);
cmd.Parameters.AddWithValue("@ProductImage", p.ProductImage);
cmd.Parameters.AddWithValue("@UnitCost", p.UnitCost);
cmd.Parameters.AddWithValue("@Description", p.Description);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
public static void DeleteProductByID(int id)
{
SqlConnection con = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand("DeleteProductByID", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ProductID", id);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary
{
public class Product:IDataErrorInfo
{
public int ProductID { get; set; }
public int CategoryID { get; set; }
public string ModelNumber { get; set; }
public string ModelName { get; set; }
public string ProductImage { get; set; }
public decimal UnitCost { get; set; }
public string Description { get; set; }
public Product(int CategoryID = 0, string ModelNumber = "",
string ModelName = "", string ProductImage = "", decimal UnitCost=0,string Description="")
{
this.CategoryID = CategoryID;
this.ModelNumber = ModelNumber;
this.ModelName = ModelName;
this.ProductImage = ProductImage;
this.UnitCost = UnitCost;
this.Description = Description;
}
public string Error
{
get { return null; }
}
public string this[string columnName]
{
get
{
bool valid = true;
if (columnName=="UnitCost")
{
if (UnitCost<0)
{
valid = false;
}
}
if (!valid)
{
return "UnitCost can not be negative";
}
else
{
return null;
}
}
}
}
}
WPF IDataErrorInfo使用-数据对象上验证的更多相关文章
- WPF中的数据验证
数据验证 WPF的Binding使得数据能够在数据源和目标之间流通,在数据流通的中间,便能够对数据做一些处理. 数据转换和数据验证便是在数据从源到目标 or 从目标到源 的时候对数据的验证和转换. V ...
- WPF使用IDataErrorInfo进行数据校验
这篇博客将介绍如何使用IDataErrorInfo进行数据校验.下面直接看例子.一个Customer类,两个属性(FirstName, Age) class Customer { public str ...
- MVC中的数据注解和验证
数据注解和验证 用户输入验证在客户端浏览器中需要执行验证逻辑. 在客户端也需要执行. 注解是一种通用机制, 可以用来向框架注入元数据, 同时, 框架不只驱动元数据的验证, 还可以在生成显示和编辑模型的 ...
- 数据注解和验证 – ASP.NET MVC 4 系列
不仅在客户端浏览器中需要执行验证逻辑,在服务器端也需要执行.客户端验证能即时给出一个错误反馈(阻止请求发送至服务器),是时下 Web 应用程序所期望的特性.服务器端验证,主要是因为来自网 ...
- WPF的ComboBox 数据模板自定义
WPF的ComboBox 有些时候不能满足用户需求,需要对数据内容和样式进行自定义,下面就简要介绍一下用数据模板(DataTemplate)的方式对ComboBox 内容进行定制: 原型设计如下: 步 ...
- WPF中的数据模板(DataTemplate)(转)
原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/30/694388.html WPF中的数据模板(DataTemplate) ...
- ASP.NET MVC5(四):数据注解和验证
前言 用户输入验证的工作,不仅要在客户端浏览器中执行,还要在服务端执行.主要原因是客户端验证会对输入数据给出即时反馈,提高用户体验:服务器端验证,主要是因为不能完全信任用户提供的数据.ASP.NET ...
- ASP.NET MVC5高级编程 之 数据注解和验证
客户端验证逻辑会对用户向表单输入的数据给出一个即时反馈.而之所以需要服务器端验证,是因为来自网络的信息都是不能被信任的. 当在ASP.NET MVC设计模式上下文中谈论验证时,主要关注的是验证模型的值 ...
- XML的应用 ---- 从一个范例看xml数据、xsd验证、xslt样式
从一个范例看XML的应用 引言 如果你已经看了Asp.Net Ajax的两种基本开发模式 这篇文章,你可能很快会发现这样一个问题:在那篇文章的方式2中,客户端仅仅是发送了页面上一个文本框的内容到服务端 ...
随机推荐
- 【codeforces 742B】Arpa’s obvious problem and Mehrdad’s terrible solution
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 无线物联网中CoAP协议的研究与实现【转】
无线物联网中CoAP协议的研究与实现 时间:2013-04-09 来源:电子科技 作者:汤春明,张 荧,吴宇平 关键字:CoAP 无线 物联网 协议 摘要:由于物联网中的很多设备都是资源受 ...
- [Angular Testing] Unit Testing -- Test component and service.
Recommend to use angular-cli to generate component and service, so we can get testing templates. ng ...
- Django之富文本编辑器kindeditor 及上传
1.什么是富文本编辑器 百度百科(https://baike.baidu.com/item/%E5%AF%8C%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8 ...
- 避免if语句的深层次嵌套
公司做了个抢红包的限制,然后ajax请求的返回字段,要进行多层逻辑的判断,想想是真恶心,虽然都是简单逻辑,而且看别人以前写的代码,发现,哎,注释能不能写上吶,像我写代码都是细致到,哪怕初学者也能看懂这 ...
- @RequiresPermissions 解释
@RequiresAuthentication 验证用户是否登录,等同于方法subject.isAuthenticated() 结果为true时. @RequiresUser 验证用户是否被记忆,us ...
- PHP/HTML混写的四种方式总结
PHP/HTML混写的四种方式总结 一.总结 一句话总结: 注意点: 1.双引号里面解析变量:echo "$Content" 2.HEREDOC和NOWDOC的关系:类似于双引号包 ...
- web.xml 中的listener、 filter、servlet 加载顺序及其详解(转)
在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人的,毕竟人家写的不错,自己也就不重复造轮子了,只是略加点了自己的修饰. 首先可以肯定的是 ...
- 【t003】string
Time Limit: 1 second Memory Limit: 50 MB [问题描述] 设有字符串X,我们称在X的头尾及中间插入任意多个空格后构成的新字符串为X的扩展串,如字符串X为" ...
- Notepad++中删除连续的任意n行
使用Notepad++里的行标记功能,可以删除指定的任意n行. 案例1,删除sample2.dat里的第201行到第10000行.方法如下: (1) 用户NotePad++打开sample2.dat, ...