静观己心,厚积薄发

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::

新建一个类,继承自:IDataErrorInfo

View Code
1 public class UserValidation : IDataErrorInfo
2 {
3 public string UserName { get; set; }
4 public string PassWord { get; set; }
5
6 public string Error
7 {
8 get
9 {
10 if (UserName.Equals(""))
11 {
12 return "错误";
13 }
14
15 if (PassWord.Length < 6)
16 {
17 return "错误";
18 }
19 else if(!PassWord.Equals("123456"))
20 {
21 return "错误";
22 }
23
24 return null;
25 }
26 }
27
28 public string this[string msg]
29 {
30 get
31 {
32 switch (msg)
33 {
34 case "UserName":
35 if (UserName.Equals(""))
36 {
37 return "用户名不能为空";
38 }
39 else
40 {
41 break;
42 }
43
44 case "PassWord":
45 if (PassWord.Length < 6)
46 {
47 return "密码长度不能小于6位";
48 }
49 else if (!PassWord.Equals("123456"))
50 {
51 return "密码错误";
52 }
53 else
54 {
55 break;
56 }
57
58 default:
59 return null;
60 }
61 return null;
62 }
63 }
64 }

Login.xaml.cs

View Code
1 public partial class Login : UserControl
2 {
3 private UserValidation uservalidation;
4
5 public Login()
6 {
7 InitializeComponent();
8
9 uservalidation = new UserValidation() { UserName = "admin", PassWord="123456" };
10 UserNameBox.DataContext = uservalidation;
11 PassWordBox.DataContext = uservalidation;
12 }
13
14 private void LoginBtn_Click(object sender, RoutedEventArgs e)
15 {
16 if ((UserNameBox.GetBindingExpression(TextBox.TextProperty).DataItem as UserValidation).Error == null)
17 {
18 this.Content = new MainPage();
19 }
20 }
21
22 private void ResetBtn_Click(object sender, RoutedEventArgs e)
23 {
24 UserNameBox.Text = "";
25 PassWordBox.Password = "";
26 }
27
28 }

Login.xaml代码:

View Code
1 <UserControl x:Class="FuSysSL.Login"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6 mc:Ignorable="d"
7 d:DesignHeight="309" d:DesignWidth="645" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
8
9 <Grid x:Name="LayoutRoot" Background="lightGray">
10 <TextBox Text="{Binding UserName,Mode=TwoWay,ValidatesOnDataErrors=True,ValidatesOnExceptions=True}" Background="{x:Null}" BorderBrush="Black" BorderThickness="1" Height="22" HorizontalAlignment="Center" Margin="150,30,112,171" x:Name="UserNameBox" Opacity="1" TextWrapping="NoWrap" VerticalAlignment="Center" Width="100" />
11 <PasswordBox Password="{Binding PassWord,Mode=TwoWay,ValidatesOnDataErrors=True,ValidatesOnExceptions=True}" Background="{x:Null}" BorderBrush="Black" BorderThickness="1" Height="22" HorizontalAlignment="Center" Margin="150,61,112,140" x:Name="PassWordBox" VerticalAlignment="Center" Width="100" />
12 <TextBlock FontSize="12" FontWeight="Bold" HorizontalAlignment="Center" Margin="100,30,218,175" Text="用户名" TextWrapping="Wrap" VerticalAlignment="Center" Width="44" />
13 <TextBlock FontSize="12" FontWeight="Bold" HorizontalAlignment="Center" Margin="100,62,221,143" Text="密 码" TextWrapping="Wrap" VerticalAlignment="Center" Width="41" />
14 <Button Click="LoginBtn_Click" Content="登 录" FontSize="12" HorizontalAlignment="Center" Margin="100,96,202,103" Name="LoginBtn" VerticalAlignment="Center" Width="60" />
15 <Button Click="ResetBtn_Click" Content="重 置" FontSize="12" HorizontalAlignment="Center" Margin="191,96,111,103" Name="ResetBtn" VerticalAlignment="Center" Width="60" />
16
17 </Grid>
18  </UserControl>

运行效果:

posted on 2011-02-14 13:25  猎人杰  阅读(699)  评论(0编辑  收藏  举报