0
我正在WPF應用程序。在我的應用程序中有保存數據的不同形式。我想在按鈕點擊或失去焦點時驗證數據。我想WPF驗證工作喜歡ASP.Net驗證。
我不知道頁面加載後表單生效。它應該僅在用戶失去焦點時或用戶單擊按鈕時進行驗證。
我在google上做了很多R & D.但我通過安裝解決方案發現了任何適當的設置。
請幫我一把。在按鈕單擊驗證WPF
我正在WPF應用程序。在我的應用程序中有保存數據的不同形式。我想在按鈕點擊或失去焦點時驗證數據。我想WPF驗證工作喜歡ASP.Net驗證。
我不知道頁面加載後表單生效。它應該僅在用戶失去焦點時或用戶單擊按鈕時進行驗證。
我在google上做了很多R & D.但我通過安裝解決方案發現了任何適當的設置。
請幫我一把。在按鈕單擊驗證WPF
在這個例子中,我向您展示與驗證兩個文本框...
XAML編碼......
<TextBlock Text="Throw Exception in Property" Grid.Column="1" Grid.Row="0" VerticalAlignment="Center" FontSize="22" Foreground="Chocolate" />
<TextBlock Text="Name " Grid.Column="0" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="15" Foreground="DarkGreen" />
<TextBlock Text="Age" Grid.Column="0" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="15" Foreground="DarkGreen" />
<TextBox Name="txtName" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" Width="170" Height="30" Background="Azure" Text="{Binding Path=pName,Mode=TwoWay,ValidatesOnExceptions=True}"/>
<TextBox Name="txtAge" Grid.Row="2" Grid.Column="2" HorizontalAlignment="Left" Width="170" Height="30" Background="Azure" Text="{Binding Path=pAge,Mode=TwoWay,ValidatesOnExceptions=True}"/>
<Button Name="btnSubmit" Content="Submit" Grid.Row="4" Height="30" Click="btnSubmit_Click"/>
C#編碼
private void Page_Loaded(object sender, RoutedEventArgs e)
{
Student s = new Student();
this.DataContext = s;
}
public class Student : INotifyPropertyChanged
{
string name;
int age;
public string pName
{
get
{
return name;
}
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("Name field must be enterd.");
}
name = value;
OnPropertyChanged("pName");
}
}
public int pAge
{
get
{
return age;
}
set
{
if(!Regex.IsMatch(value.ToString(),"[0-9]"))
{
throw new ArgumentException("Age field must be in number.");
}
if(value < 18 || value > 100)
{
throw new ArgumentException("Age field must be 18-100.");
}
age = value;
OnPropertyChanged("pAge");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string data)
{
if (PropertyChanged != null)
{
PropertyChanged(this,new PropertyChangedEventArgs(data));
}
}
}
這段代碼在不工作點擊按鈕。 – jellysaini
給我你的應用程序,我將把驗證併發回給你... –