wpf devexpress实现输入验证使用验证规则

打开此项目

目标是一个registration form行为像google registration form。打开Google registration form 研究它的行为。当form是第一次显示,它的"Register"按钮应该启动;编辑器没有提示任何输入错误。输入First Name编辑器字段,清理输入和离开编辑器应该显示的First Name编辑器在错误状态。

在此教程,你可以实现本行为。

最好的审核实现行为是绑定验证规则-一个标准审核被WPF平台提供。在之前继续,审核如下MSDN文档主题:如何实现绑定验证。

可以创建一个验证规则提示一个输入错误对于每一个空编辑器字段添加普通文件夹在项目和一个新文件,ValidationRule.cs,定义如下类

cs 复制代码
public class RequiredValidationRule : ValidationRule {
    public static string GetErrorMessage(string fieldName, object fieldValue, object nullValue = null) {
        string errorMessage = string.Empty;
        if(nullValue != null && nullValue.Equals(fieldValue))
            errorMessage = string.Format("You cannot leave the {0} field empty.", fieldName);
        if(fieldValue == null || string.IsNullOrEmpty(fieldValue.ToString()))
            errorMessage = string.Format("You cannot leave the {0} field empty.", fieldName);
        return errorMessage;
    }
    public string FieldName { get; set; }
    public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
        string error = GetErrorMessage(FieldName, value);
        if(!string.IsNullOrEmpty(error))
            return new ValidationResult(false, error);
        return ValidationResult.ValidResult;
    }
}

RequiredValidationRule类检测输入值从继承Validate 方法。对于null或者空编辑值,此方法返回一个审核错误消息。

确定使用验证规则,替换RequiredValidationRule实例在Binding.ValidationRules集合在如下。

XML 复制代码
<UserControl xmlns:Common="clr-namespace:RegistrationForm.Common" ... >
    ...
    <dxe:TextEdit NullText="FIRST">
        <dxe:TextEdit.EditValue>
            <Binding Path="FirstName" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay">
                <Binding.ValidationRules>
                    <Helpers:RequiredValidationRule FieldName="First Name"/>
                </Binding.ValidationRules>
            </Binding>
        </dxe:TextEdit.EditValue>
    </dxe:TextEdit>
    ...
</UserControl>

Binding.UpdateSourceTrigger属性设置PropertyChanged - 默认值LostFocus。在LostFocus值之上,验证进程只有当结束时调用-用户离开编辑器。

在EditValue属性可用在任何DXEditors说明绑定机制,考虑如下简单的绑定:EditValue="{Binding FristName,Mode=TwoWay}".

EditValue 是目标属性当绑定应用。对于必须,目标属性总是DependencyProperty(依赖属性)

FirstName属性是源属性。绑定目标属性到源属性。源可以是一个DenpendencyProperty。一个恒定属性或属性触发INotifyProertyChanged.PropertyChanged事件当新值设置。此源属性无论如何不能是字段-必要属性。

绑定可以使用双向绑定模式同样传递输入数据在目标,到密封的潜在源数据。双向绑定行为,通过默认当束缚控件丢失焦点更新源属性。此刷新行为可以被UpdateSourceTrigger指定。对于实例,设置UpdateSourceTrigger参数到PropertyChanged,更新源属性作为目标属性刷新。参考此信息:Binding.UpdateSourceTrigger

此时,将您的注意力转向 DXEditors 如何修改 BaseEdit.EditValue 属性。

相关推荐
c#上位机1 小时前
wpf之命令
c#·wpf
没有bug.的程序员1 小时前
分布式链路追踪:微服务可观测性的核心支柱
java·分布式·微服务·架构·wpf
Aevget20 小时前
DevExpress WPF中文教程:Data Grid - 如何使用虚拟源?(一)
c#·wpf·界面控件·devexpress·ui开发
The Sheep 20233 天前
WPF自定义路由事件
大数据·hadoop·wpf
阳光雨滴3 天前
使用wpf用户控件编程落石效果动画
c++·wpf
wuty0073 天前
WPF 调用 ChangeWindowMessageFilterEx 修改指定窗口 (UIPI) 消息筛选器的用户界面特权隔离
wpf·sendmessage·changewindowmessagefilterex·uip·消息筛选器的用户界面特权隔离·window message
攻城狮CSU3 天前
WPF中核心接口 INotifyPropertyChanged
wpf
c#上位机3 天前
wpf之Interaction.Triggers
c#·wpf
是木子啦3 天前
wpf passwordbox控件 光标移到最后
c#·wpf
The Sheep 20233 天前
wpf 命令理解
wpf