wpf IDataErrorInfo 验证

我可以给你一个简单的例子来说明如何在WPF中使用IDataErrorInfo接口来显示错误信息,并通过样式(Style)和触发器(Trigger)来在UI上反映这些错误。

首先,你需要一个实现了IDataErrorInfo接口的模型类。这个接口要求你实现两个方法:Error(通常用于整个对象的错误,但在这个例子中我们可能不会用到它)和this[string columnName](用于获取指定属性的错误信息)。

cs 复制代码
public class MyModel : IDataErrorInfo  
{  
    private string _propertyName;  
  
    public string PropertyName  
    {  
        get { return _propertyName; }  
        set  
        {  
            _propertyName = value;  
            // 这里可以添加属性更改通知,但在这个例子中我们省略它  
        }  
    }  
  
    public string Error => throw new NotImplementedException(); // 通常不实现,除非有全局错误  
  
    public string this[string columnName]  
    {  
        get  
        {  
            string error = string.Empty;  
            if (columnName == nameof(PropertyName))  
            {  
                if (string.IsNullOrEmpty(PropertyName))  
                {  
                    error = "PropertyName cannot be empty.";  
                }  
                // 可以添加更多验证逻辑  
            }  
            return error;  
        }  
    }  
}

然后,在XAML中,你需要为绑定到该模型的UI元素(如TextBox)设置一些样式和触发器,以便在验证失败时显示错误信息。但是,请注意,Validation.Errors集合本身并不直接支持数据绑定到UI元素(如TextBlock的Text属性)。相反,我们通常使用Validation.HasError附加属性来触发样式更改或显示错误模板。

以下是一个简单的XAML示例,展示了如何为TextBox设置样式,以便在验证失败时更改其边框颜色:

cs 复制代码
<Window x:Class="YourNamespace.MainWindow"  
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
        Title="MainWindow" Height="350" Width="525">  
    <Window.Resources>  
        <Style x:Key="ErrorTextBoxStyle" TargetType="TextBox">  
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>  
            <Style.Triggers>  
                <Trigger Property="Validation.HasError" Value="True">  
                    <Setter Property="BorderBrush" Value="Red"/>  
                    <Setter Property="BorderThickness" Value="2"/>  
                    <Setter Property="ToolTip"  
                            Value="{Binding RelativeSource={RelativeSource Self},  
                                            Path=(Validation.Errors)[0].ErrorContent}"/>  
                </Trigger>  
            </Style.Triggers>  
        </Style>  
    </Window.Resources>  
  
    <Grid>  
        <TextBox Text="{Binding Path=PropertyName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"  
                 Style="{StaticResource ErrorTextBoxStyle}"  
                 Margin="10"/>  
    </Grid>  
</Window>
相关推荐
Zhen (Evan) Wang11 小时前
.NET 6 + Dapper + User-Defined Table Type
sqlserver·c#·.net·wpf
界面开发小八哥1 天前
界面控件DevExpress WPF v25.1预览 - 支持Windows 11系统强调色
windows·wpf·界面控件·devexpress·ui开发·.net 9
军训猫猫头1 天前
89.WPF 中实现便捷的数字输入框:DecimalUpDown 控件的使用 WPF例子 C#例子.
开发语言·c#·wpf
Pasregret1 天前
缓存与数据库一致性深度解析与解决方案
数据库·缓存·wpf
Java林间3 天前
Zookeeper是什么?基于zookeeper实现分布式锁
分布式·zookeeper·wpf
zizisuo4 天前
1.微服务拆分与通信模式
微服务·wpf
程序员秘密基地4 天前
基于c#,wpf,ef框架,sql server数据库,音乐播放器
sql·sqlserver·c#·.net·wpf
Zhen (Evan) Wang4 天前
.NET 6 WPF 利用CefSharp.Wpf.NETCore显示PDF文件
.net·wpf·.netcore
冰茶_5 天前
WPF特性分析
学习·microsoft·c#·wpf
qq_196055875 天前
最快打包WPF 应用程序
wpf