WPF数据验证之BindingGroup
BindingGroup 类
包含用于验证对象绑定和 ValidationRule 对象的集合。
主要用到的函数: BeginEdit() 开始在源中编辑事务在 BindingGroup。
CancelEdit() 关闭编辑事务并放弃挂起的更改。
主要原理(个人理解,可能有偏差):通过BeginEdit()开启一个事务,提交数据之后,挂起更改,获取当前的数据,并对其内容进行验证,若通过验证,则提交事务;若失败则调用CancelEdit() 关闭编辑事务并放弃挂起的更改。
用途:在提交数据或者登录时,需要对多项数据进行验证时,可以考虑用BindingGroup;
<Window x:Class="WpfApplication_BingGroup.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication_BingGroup"
Title="BindingGroup" Height="247" Width="351">
<Grid>
<StackPanel Name="stackPanel1" >
<StackPanel.BindingGroup>
<BindingGroup NotifyOnValidationError="True">
<BindingGroup.ValidationRules>
<local:Validatess ValidationStep="ConvertedProposedValue"></local:Validatess>
</BindingGroup.ValidationRules>
</BindingGroup>
</StackPanel.BindingGroup>
<TextBox Height="23" Name="textBox1" Text="{Binding UserName, UpdateSourceTrigger=LostFocus}" Width="120" Margin="0,30,0,0" />
<TextBox Height="23" Name="textBox2" Text="{Binding PassWord, UpdateSourceTrigger=LostFocus}" Width="120" Margin="0,20,0,0" />
<StackPanel Height="37" Name="stackPanel2" Width="165" Margin="0,20,0,0" Orientation="Horizontal">
<Button Content="提交" Height="23" Name="button1" Width="60" Margin="10,0,0,0" Click="button1_Click" />
<Button Content="取消" Height="23" Name="button2" Width="60" Margin="20,0,0,0" Click="button2_Click" />
</StackPanel>
</StackPanel>
</Grid>
</Window>
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//绑定数据源
this.stackPanel1.DataContext = new UserInfo();
//开启一个事务
this.stackPanel1.BindingGroup.BeginEdit();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
if (this.stackPanel1.BindingGroup.CommitEdit())
{
MessageBox.Show("提交成功");
this.stackPanel1.BindingGroup.BeginEdit();
}
MessageBox.Show("提交失败");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
//取消该事务
this.stackPanel1.BindingGroup.CancelEdit();
//重新启动一个事务
this.stackPanel1.BindingGroup.BeginEdit();
}
}
public class Validatess : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
//获取对象
BindingGroup bg = value as BindingGroup;
UserInfo ui = bg.Items[0] as UserInfo;
//验证条件,此处判断字符串的长度是否大于5
if(ui.UserName.Length>5&&ui.PassWord.Length>5)
{
return new ValidationResult(true,null);
}
return new ValidationResult(false,"验证错误");
}
}
public class UserInfo:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string userName;
public string UserName
{
get { return userName; }
set { userName = value;
PropertyChanged(this, new PropertyChangedEventArgs("UserName"));
}
}
private string passWord;
public string PassWord
{
get { return passWord; }
set { passWord = value;
PropertyChanged(this, new PropertyChangedEventArgs("PassWord"));
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步