简体   繁体   English

如何在WPF页面(局部类)中实现IDataErrorInfo

[英]How to Implement IDataErrorInfo within a WPF Page (A Partial Class)

It's my first experience with WPF. 这是我第一次使用WPF。 And I'm developing an Inventory Management System. 而且我正在开发一个库存管理系统。 My design model has single Window, in which a frame loads different Pages while clicking on different buttons. 我的设计模型只有一个Window,其中一个框架在单击不同按钮时会加载不同的Pages。 While adding a new Inventory into the Database, I want to ensure Data Validation. 在向数据库中添加新清单时,我要确保数据验证。 I choose IDataErrorInfo in this regard. 在这方面,我选择IDataErrorInfo。 I have to implement the interface but unable to implement just writing as public partial class AddInventoryPage : Page, IDataErrorInfo . 我必须实现该接口,但public partial class AddInventoryPage : Page, IDataErrorInfo仅将其编写为public partial class AddInventoryPage : Page, IDataErrorInfo This shows error. 这显示错误。 The signature of the class is as following 该类的签名如下

public partial class AddInventoryPage : Page

I also tried as under but unable to achieve the functionality. 我也尝试了下但无法实现该功能。 Even I put a breakpoint within IDataErrorInfor part but the control doesn't go there. 甚至我在IDataErrorInfor部分中都设置了一个断点,但是控件没有移到那里。

namespace IMS
{
    public partial class AddInventoryPage : IDataErrorInfo
    {
    //code here
    }
    public partial class AddInventoryPage : Page
    {
    //code here
    }
}

As My Inventory module is completed except Data Validation, and I'm working on the Sales module; 由于除了数据验证外,我的库存模块已经完成,因此我正在开发销售模块。 it's not a solution to change my design model. 这不是更改我的设计模型的解决方案。 Moreover, I'm not using any Design Pattern like MVVM. 而且,我没有使用像MVVM这样的任何设计模式。 It's straight. 是直的 Looking forward to a solution. 期待解决方案。

example with a validation against the property 'Name' 对属性“名称”进行验证的示例

public class AddInventoryPage : IDataErrorInfo
{
    public string Name { get; set; }       

    public string Error => null;

    public string this[string columnName]
    {
        get
        {
            switch(columnName)
            {
                case nameof(Name):
                    if (Name == string.Empty) return "Name can not be empty";
            }

            return string.Empty;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM