【问题标题】:auto-update calculated property in WPF datagrid自动更新 WPF 数据网格中的计算属性
【发布时间】:2021-10-13 03:39:53
【问题描述】:

我有一个 TestModel 类,它有一个 FirstName 和一个 LastName 属性以及一个“计算属性”,它没有设置器,但只是以“Lastname, Firstname”的格式返回 FullName。

public class TestModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName
    {
        get
        {
            return $"{LastName}, {FirstName}";
        } 
    }

}

我将此类的多个对象存储在 ObservableCollection 中,它是 WPF 应用程序中数据网格的 ItemsSource。当我通过 GUI 添加项目时,它们立即出现在网格中,一切都很好。

但是,例如,当我更改网格中一个特定对象的 FirstName 时,FullName 不会自动更改(实际上它确实会更改,但更改不会反映在 GUI 中)。 我必须在这个类上实现 INotifyPropertyChanged 吗?如果是这样,我该怎么做?我读了一些例子,但我必须承认我无法将它们转移到我的案例中......

【问题讨论】:

    标签: c# wpf observablecollection


    【解决方案1】:

    您必须强制 INotifyPropertyChanged 并确保在您的 FullNameproperty 更改时调用该事件。当您的 FirstName 或您的 LastName 属性发生更改时就是这种情况。

    public class TestModel : INotifyPropertyChanged
        {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _firstName;
        public string FirstName { get => _firstName;
        set
        {
            _firstName = value;
            InvokePropertyChanged();
            InvokePropertyChanged(nameof(FullName));
        }
    
        private _string _lastName;    
        public string LastName
        {
           get => _lastName;
           set
           {
              _lastName= value;
              InvokePropertyChanged();
              InvokePropertyChanged(nameof(FullName));
           }
        }
    
       public string FullName
       {
           get
           {
              return $"{LastName}, {FirstName}";
           } 
        }
    
        private void InvokePropertyChanged([CallerMemberName]string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    【讨论】:

      【解决方案2】:

      是的,您需要在视图模型中实现 INotifyPropertyChanged。

      示例:

      public class ViewModelBase: INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
          protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
          {
              var handler = PropertyChanged;
              if (handler != null)
              {
                  handler(this, e);
              }
          }
          protected void RaisePropertyChanged(String propertyName)
          {
              VerifyPropertyName(propertyName);
              OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
          }
      }
      

      像这样将它添加到您的程序代码中:

      public class Testmodel : ViewModelBase
      {
          private string _FistName;
          public string FirstName
          {
              get
              {
                  return _FistName;
              }
              set
              {
                  if(_FistName == value)
                  {
                      return;
                  }
                  _FistName = value;
                  RaisePropertyChanged("FirstName");
                  RaisePropertyChanged("FullName");
              }
          }
          private string _LastName;
          public string LastName
          {
              get
              {
                  return _LastName;
              }
              set
              {
                  if (_LastName == value)
                  {
                      return;
                  }
                  _LastName = value;
                  RaisePropertyChanged("LastName");
                  RaisePropertyChanged("FullName");
              }
          }
          public string FullName
          {
              get
              {
                  return $"{LastName}, {FirstName}";
              }
          }
      }
      

      如果您现在将值设置为 FirstName 或 LastName,它应该引发更新 gui 的 PropertyChanged 事件。试试看:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-17
        • 2012-01-27
        • 1970-01-01
        • 2017-11-02
        • 2019-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多