关于数据验证的问题!
LINQ后台模型代码,对于字段,我加上了[Required(ErrorMessage="必填!")]
并且在Set里加上了验证Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "CarNo" });
在XAML文件里的Textbox如下:
<TextBox Height="35" Text="{Binding CarNo,Mode=TwoWay,ValidatesOnExceptions=True, NotifyOnValidationError=True}" Name="textBox1" Width="137" />
后台代码:
public Home() { InitializeComponent(); this.Loaded += new RoutedEventHandler(ValidationSummary_Loaded); } void ValidationSummary_Loaded(object sender, RoutedEventArgs e) { WebServiceSoapClient client = new WebServiceSoapClient(); client.GetBAS_CarListCompleted += new EventHandler<GetBAS_CarListCompletedEventArgs>(GetBAS_CarListCompleted); client.GetBAS_CarListAsync(); } void GetBAS_CarListCompleted(object sender,GetBAS_CarListCompletedEventArgs e) { this.LayoutRoot.DataContext = e.Result; } private void button1_Click(object sender, RoutedEventArgs e) { BindingExpression BE = this.textBox1.GetBindingExpression(TextBox.TextProperty); BE.UpdateSource(); WebServiceSoapClient client = new WebServiceSoapClient(); client.AddBAS_CarAsync(this.textBox1.Text, 1, "123", "123", 12, 12, 12, 12, 12, "123", "123213", 1, System.DateTime.Now); }
if (this.validationSummary1.HasErrors){ // 停止执行}else{ // 继续执行}