Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Propertygrid user input data validation issue

未关闭
#1,772 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
需要澄清
活跃度
停滞
技术栈
csharp
领域
desktop

调研方向

Start with the supplied WPF reproduction at XctkPropertyGrid and PropertyGrid_Loaded, using the Person and Person2 examples. Trace how PropertyGrid handles IDataErrorInfo and DataAnnotations validation during editing and focus loss. No repository file or test is identified; done means invalid input shows appropriate feedback and the error state clears when the value becomes valid.

由索引模型根据 Issue 内容生成。

描述

question1: [Range()] causes the UI to have no user error prompts
//test example as follow:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.CompilerServices;
namespace WpfApp1
{
public class Person: INotifyPropertyChanged, IDataErrorInfo
{
[Required(ErrorMessage = "Name is Required")]
[DisplayName("Name")]
public string Name { set; get; }

    [Range(0,150,ErrorMessage = "Age= 0~150")]
    [DisplayName("Age")]
    public int Age { set; get; }

    public event PropertyChangedEventHandler? PropertyChanged;

    [Browsable(false)]
    public string Error { get; }

    public  string this[string propertyName]
    {
        get
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentException("Invalid property name", propertyName);
            }
            string error = string.Empty;
            var val = GetType().GetProperty(propertyName).GetValue(this, null);
          
            var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>(1);
            var result = Validator.TryValidateProperty(val, new ValidationContext(this, null, null)
                { MemberName = propertyName }, results);
            if (!result)
            {
                var validationResult = results.First();
                error = validationResult.ErrorMessage;
            }
            
            return error;
        }
    }

    protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) => this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
   
    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
    {
        PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if (propertyChanged == null)
            return;
        propertyChanged((object)this, args);
    }
}

}



<xctk:PropertyGrid Name="XctkPropertyGrid" Loaded="PropertyGrid_Loaded"></xctk:PropertyGrid>

private void PropertyGrid_Loaded(object sender, RoutedEventArgs e)
{
Person p = new Person(){Age = 20,Name = "wala"};

        this.XctkPropertyGrid.SelectedObject = p;
    }

[Range (0150, ErrorMessage="Age=0-150")] can prevent users from entering illegal values, but there is no red box mark or error prompt. Debugging found that this [string propertyName] indexer has not been called because it prevents users from entering illegal values.

question2:use person2 insteadof person1,When the user inputs an incorrect value, a red boder will prompt, but when the user leaves the attribute input box, the value in the red boder will return to the legal value, but the red boder will not disappear.
public class Person2 : INotifyPropertyChanged, IDataErrorInfo
{

    [DisplayName("Name")]
    public string Name { set; get; }

    private int _age;
    [DisplayName("Age")]
    public int Age {
        set
        {
            if (!ValidataProperty(nameof(Age), value))
                return;
                _age = value;
            RaisePropertyChanged(nameof(Age));
        }
         get
        {
            return _age;
        }}

    public event PropertyChangedEventHandler? PropertyChanged;

    [Browsable(false)]
    public string Error { get; }

    protected bool ValidataProperty(string propertyName, object val)
    {

        if (propertyName.Equals(nameof(Age)))
        {
            int ageVal = (int)val;
            if (ageVal < 0 || ageVal > 150)
            {
                ErrorProperty = propertyName;
                ErrorMsg = "Age =0~150";
                return false;
            }
                
        }

        ErrorMsg = string.Empty;
        ErrorProperty = string.Empty;
        return true;
    }

    private string ErrorProperty;
    private string ErrorMsg;

    public string this[string propertyName]
    {
        get
        {
            if (ErrorProperty.Equals(propertyName))
            {
                return ErrorMsg;
            }

            return string.Empty;
        }
    }

    protected void RaisePropertyChanged([CallerMemberName] string propertyName = null) => this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
    {
        PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if (propertyChanged == null)
            return;
        propertyChanged((object)this, args);
    }
}
主要语言
C#
星标
4.2k
派生
912
PR 合并指标
30 天内没有已合并 PR

贡献指南

这个仓库没有索引到贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

xceedsoftware/wpftoolkit 的其他 Issue

查看 xceedsoftware/wpftoolkit 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。