2012-07-09 81 views
1

我遇到麻烦验证字段。 我有一个指定的名为Test的属性,其代码在 以下时发生异常,当它小于零但验证不起作用时。 我正在使用从视图调用的Web服务。 我想我忘了包括一些东西,但我不知道是什么。Silverlight验证不起作用

非常感谢。

页:

namespace MonitorizacionIncidencias.Views 
{ 
    public partial class TESTING : Page 
    { 
     private IncidenciasServiceClient proxy = new IncidenciasServiceClient(); 

     public TESTING() 
     { 
      InitializeComponent(); 

      proxy.NextCompleted += new EventHandler<NextCompletedEventArgs>(proxy_NextCompleted); 
      proxy.NextAsync(null, 9, false); 
     }   

     void proxy_NextCompleted(object sender, NextCompletedEventArgs e) 
     { 
      DataContext = e.Result;   
     } 

    } 
} 

XAML:

<TextBox Text="{Binding TEST, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Height="23" HorizontalAlignment="Left" Margin="243,283,0,0" x:Name="textBox2" VerticalAlignment="Top" Width="120" /> 

型号:

[DataContract] 
public class Incidencia 
{ 

     [DataMember] 
     public int TEST 
     { 
      get 
      { 
       return test; 
      } 
      set 
      { 
       if (value < 0) 
        throw new Exception("TEST EXCP"); 

       test = value; 
      } 
     } 
} 
+0

+1与代码写清楚问题&XAML(让生活变得如此简单) – 2012-07-09 13:19:43

回答

0

你正在做的大部分,但你AR它实际上不会处理它生成的BindingValidationError event

例如在XAML中添加此(即适用于文本框或它的父容器):

BindingValidationError="MyBindingValidationError" 

,并把您的实际处理在这里:

private void MyBindingValidationError(object sender, 
    ValidationErrorEventArgs e) 
{ 
    if (e.Action == ValidationErrorEventAction.Added) 
    { 
     textBox2.Background = new SolidColorBrush(Colors.Red); 

    } 
    else if (e.Action == ValidationErrorEventAction.Removed) 
    { 
     textBox2.Background = new SolidColorBrush(Colors.White); 
    } 
} 
+0

谢谢这很有用。 – MirlvsMaximvs 2012-07-13 09:46:36