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>
相关推荐
xcLeigh14 小时前
WPF跨平台开发探讨:借助相关技术实现多平台应用
c#·wpf
埃菲尔铁塔_CV算法1 天前
WPF 开发从入门到进阶(五)
深度学习·算法·机器学习·计算机视觉·wpf
SongYuLong的博客1 天前
C# WPF编程-Menu
开发语言·c#·wpf
SongYuLong的博客1 天前
C# WPF编程-边框控件(Border)
开发语言·c#·wpf
加号32 天前
【WPF】c#读取CAD的dxf文件,并基于Canvas将读取到的数据重新描绘到界面
c#·wpf
SongYuLong的博客2 天前
C# WPF编程-启动新窗口
microsoft·c#·wpf
Now喔3 天前
封装WPF中转换器常用用法封装
wpf
她说彩礼65万4 天前
WPF Prism事件聚合器EventAggregator
wpf
源之缘-OFD先行者4 天前
基于WPF的雷达上位机系统开发实践
wpf·上位机·雷达
xcLeigh5 天前
WPF未来展望:紧跟技术发展趋势,探索新的可能性
c#·wpf