Как настроить проверку свойств для xceed. propertyGrid?C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Как настроить проверку свойств для xceed. propertyGrid?

Сообщение Anonymous »


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(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); } } } part xaml code as follow:

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 border will prompt, but when the user leaves the attribute input box, the value in the red border will return to the legal value, but the red border 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); } }

Источник: https://stackoverflow.com/questions/780 ... opertygrid
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Не удалось загрузить файл или сборку «Xceed.Wpf.Toolkit».
    Anonymous » » в форуме C#
    0 Ответы
    25 Просмотры
    Последнее сообщение Anonymous
  • Не удалось загрузить файл или сборку «Xceed.Wpf.Toolkit».
    Anonymous » » в форуме C#
    0 Ответы
    12 Просмотры
    Последнее сообщение Anonymous
  • Кнопка в неправильном поведении PropertyGrid
    Anonymous » » в форуме C++
    0 Ответы
    11 Просмотры
    Последнее сообщение Anonymous
  • Создать пользовательскую форму CollectionEditor для использования с PropertyGrid?
    Anonymous » » в форуме C#
    0 Ответы
    19 Просмотры
    Последнее сообщение Anonymous
  • Как я могу вставить EventsTab в PropertyGrid?
    Anonymous » » в форуме C#
    0 Ответы
    7 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C#»