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>
相关推荐
code bean17 小时前
【WPF】WPF 项目实战:构建一个可增删、排序的光源类型管理界面(含源码)
wpf
沉到海底去吧Go21 小时前
【图片识别改名】如何批量将图片按图片上文字重命名?自动批量识别图片文字并命名,基于图片文字内容改名,WPF和京东ocr识别的解决方案
ocr·wpf·图片识别改名·图片识别重命名·图片内容改名
lph19721 天前
自定义事件wpf
wpf
code bean1 天前
【WPF】从普通 ItemsControl 到支持筛选的 ItemsControl:深入掌握 CollectionViewSource 用法
wpf
碎碎念的安静2 天前
WPF可拖拽ListView
c#·wpf
界面开发小八哥2 天前
界面组件DevExpress WPF中文教程:Grid - 如何识别行和卡片?
.net·wpf·界面控件·devexpress·ui开发
TwilightLemon3 天前
WPF 使用CompositionTarget.Rendering实现平滑流畅滚动的ScrollViewer,支持滚轮、触控板、触摸屏和笔
wpf
Vae_Mars5 天前
WPF中自定义消息弹窗
wpf
Magnum Lehar5 天前
GameEngine游戏引擎前端界面wpf页面实现
前端·游戏引擎·wpf
TA远方5 天前
【C#】一个简单的http服务器项目开发过程详解
服务器·http·c#·wpf·web·winform·console