(转)Silverlight4 Data Validation

原文地址:http://www.cnblogs.com/PerfectSoft/archive/2012/02/26/2369118.html

 

 

数据有效性验证时非常的重要,对于所有的应用程序而言,都是必不可少的。在将用户输入的数据进行保存之前,务必对其进行有效性验证,确保所有相关数据有效后,方可保存。

   
在Silverlight中,默认情况下,Silverlight不会对用户输入的无效数据给出友好的提示,以帮助用户输入正确的数据。幸运的是,Silverlight提供了进行数据有效性验证的特性,只需做一些额外的处理即可。


    没有任何数据有效性验证的数据类:


    //没有任何数据有效性验证
public class Student
{
private string name;
private int age;

public string Name
{
get { return name; }
set { name = value; } //未进行有效性验证,可能为空Null、空白字符......
}

public int Age
{
get { return age; }
set { age = value; } //未进行有效性验证,可能小于0或大于100等.........
}
}


    MainPage XAML:


<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<TextBlock Text="姓名:" HorizontalAlignment="Right"></TextBlock>

<TextBox x:Name="NameTxt" Text="{Binding Name, Mode=TwoWay, TargetNullValue='姓名不能为空', FallbackValue='N/A'}"
Width="300" HorizontalAlignment="Left" Grid.Column="1"></TextBox>

<TextBlock Text="年龄:" HorizontalAlignment="Right" Grid.Row="1"></TextBlock>

<TextBox x:Name="AgeTxt" Text="{Binding Age, Mode=TwoWay, TargetNullValue='年龄不能小于1', FallbackValue='N/A'}"
Width="300" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left"></TextBox>

<Button x:Name="Submit" Content="Submit" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" Width="100"></Button>
</Grid>


    MainPage Code:



    此时运行Application,结果如下:



   在年龄文本框中输入"abc“:




  • ValidationOnException

   
ValidationOnException = True,这将使Silverlight提供一个可视化的反馈信息当一个无效信息存在,这个属性将告诉Silverlight数据绑定引擎进行数据有效性验证,在进行数据对象赋值时。


    在进行验证时,有以下几点需要注意:



  • DataBinding must be TwoWay
  • ValidationOnException = True
  • 当目标对象失去焦点时,目标属性值更新至源属性

    带有数据有效性验证的数据类:


    //带有数据有效性验证
public class Student
{
private string name;
private int age;

public string Name
{
get { return name; }
set { name = value; }
}

public int Age
{
get { return age; }
set
{
//进行数据有效性验证
if (age < 0 || age > 100)
{
throw new Exception("年龄不能小于0或大于100");
}
age = value;
}
}
}


    MainPage XAML:


<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<TextBlock Text="姓名:" HorizontalAlignment="Right" VerticalAlignment="Center"></TextBlock>

<TextBox x:Name="NameTxt" Text="{Binding Name, Mode=TwoWay, ValidatesOnExceptions=True, TargetNullValue='姓名不能为空', FallbackValue='N/A'}"
Width="300" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1"></TextBox>

<TextBlock Text="年龄:" HorizontalAlignment="Right" Grid.Row="1" VerticalAlignment="Center"></TextBlock>

<TextBox x:Name="AgeTxt" Text="{Binding Age, Mode=TwoWay,ValidatesOnExceptions=True, TargetNullValue='年龄不能小于1', FallbackValue='N/A'}"
Width="300" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"></TextBox>

<Button x:Name="Submit" Content="Submit" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" Width="100"></Button>
</Grid>


    运行Application,在年龄文本框中输入abc:



   输入200:




  • The BindingValidationFailed Event

    NotifyOnValidationError =
True,这使Silverlight
数据绑定引擎激发BindingValidationFailed当在数据绑定过程中发生异常时,BindingValidationFailed事件是一个冒泡事件,所以此事件最好是在所有表单元素的第一个共同父元素上绑定。所以在此示例中最好是在Grid元素上激发BindingValidationFailed事件。


    MainPage XAML:


<Grid x:Name="LayoutRoot" Background="White" BindingValidationError="LayoutRoot_BindingValidationError">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="50" />
<RowDefinition Height="50" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<TextBlock Text="姓名:" HorizontalAlignment="Right" VerticalAlignment="Center"></TextBlock>

<TextBox x:Name="NameTxt" Text="{Binding Name, Mode=TwoWay, ValidatesOnExceptions=True,NotifyOnValidationError=True, TargetNullValue='姓名不能为空', FallbackValue='N/A'}"
Width="300" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1"></TextBox>

<TextBlock Text="年龄:" HorizontalAlignment="Right" Grid.Row="1" VerticalAlignment="Center"></TextBlock>

<TextBox x:Name="AgeTxt" Text="{Binding Age, Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True, TargetNullValue='年龄不能小于1', FallbackValue='N/A'}"
Width="300" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"></TextBox>

<Button x:Name="Submit" Content="Submit" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" Width="100"></Button>
</Grid>


    BindingValidateOnError Event:



    其实去呈现异常信息,还可以采用ValidationSummary控件,与Asp.net中的类似。



  • UpdateSourceTrigger

   
UpdateSourceTrigger取值为:Defualt[默认值]、Explicit,当UpdateSourceTrigger =
Explicit时,必须手动更新源属性的值。


 <TextBox x:Name="NameTxt" Text="{Binding Name, Mode=TwoWay, ValidatesOnExceptions=True,NotifyOnValidationError=True,UpdateSourceTrigger=Explicit, TargetNullValue='姓名不能为空', FallbackValue='N/A'}"
Width="300" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1"></TextBox>
<TextBox x:Name="AgeTxt" Text="{Binding Age, Mode=TwoWay,ValidatesOnExceptions=True,NotifyOnValidationError=True, UpdateSourceTrigger=Explicit, TargetNullValue='年龄不能小于1', FallbackValue='N/A'}"
Width="300" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center"></TextBox>


   现在,手动进行源属性的更新,通过点击Submit按钮:


        private void Submit_Click(object sender, RoutedEventArgs e)
{
TextBox tempBox = null;

foreach (var control in LayoutRoot.Children)
{
tempBox = control as TextBox;
if (tempBox != null)
{
tempBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
}
}
}


    在进行项目开发的时候,如果在WebService中的数据类执行有效性验证,在客户端一直没有验证成功过,且会抛出异常,针对这个问题各位有什么好的解决方案,请告诉我下,服务端数据类如下所示:


    [DataContract()]
public class Student : INotifyPropertyChanged
{
private string name;
private int age;

//实现INotifyPropertyChanged接口
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}

[DataMember()]
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

[DataMember()]
public int Age
{
get
{
return age;
}
set
{
//有效性验证从未成功过
if (age < 0 || age > 100)
{
throw new Exception("年龄不能小于0或大于100");
}
age = value;
}
}
}


如果解决???



posted on 2012-02-29 11:55  黑子范  阅读(144)  评论(0编辑  收藏  举报

导航