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 属性。

相关推荐
dotent·14 小时前
一个 WPF 文档和工具窗口布局容器
wpf
c#上位机14 小时前
wpf之ComboBox
wpf
lindexi18 小时前
WPF 引用 ASP.NET Core 的 AOT 版本
wpf·asp.netcore
我好喜欢你~1 天前
WPF---数据模版
wpf
hqwest2 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest3 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple3 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题4 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘4 天前
开发WPF项目时遇到的问题总结
wpf
hqwest5 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel