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

相关推荐
BearHan24 分钟前
非常'肤浅'的理解MVVM
wpf
ou.cs3 小时前
wpf 控件开发中,OnApplyTemplate 和 OnContentRendered区别
c#·.net·wpf
ou.cs1 天前
wpf 队列(Queue)在视觉树迭代查找中的作用分析
wpf
code bean1 天前
【WPF】WPF 中 `DisplayMemberPath` 与 `SelectedValuePath` 的深入理解与实战应用
windows·wpf
Magnum Lehar2 天前
wpf3d游戏引擎EditorColors.xaml实现
ui·游戏引擎·wpf
ou.cs2 天前
wpf 解决DataGridTemplateColumn中width绑定失效问题
c#·wpf
Magnum Lehar3 天前
wpf3d游戏引擎ControlTemplate.xaml.cs文件实现
游戏引擎·wpf
Magnum Lehar3 天前
wpf游戏引擎前端的Transform.cs实现
前端·游戏引擎·wpf
@Crazy Snail3 天前
WPF数据绑定疑惑解答--(关于控件的Itemsource,Collection绑定)
windows·wpf·wpf数据绑定
猕员桃3 天前
《Elasticsearch 分布式搜索在聊天记录检索中的深度优化》
分布式·elasticsearch·wpf