使 class 的 children 相互通信

Making the children of a class communicate witch eachtother

我的 MVVM 的 ViewModel 有问题 pattern-code。

我有一堆测量和一堆规则来评估测量,存储在 classes 规则和测量中。在我的主要 class MyClass 中,我将我的规则和测量存储在 ObservableCollections (OC) 中(并连接到 DataGrid)。

对于所有 n 个规则,我在一个 OC 中创建了 n 个 CollcetionOfEvaluators,并将相应的规则和所有测量值传递给每个单独的规则。 在每个 CollectionOfEvaulators 中,我为 OC 中的一个规则和 m 个测量 m 个评估器创建。

评估者接受一个规则和一个测量,如果相应的测量是否通过相应的规则,则返回一个布尔值。

然后我有一个 ListView,它为每个规则显示一个 DataGrid,如果它通过规则,则为每个测量显示。

我的问题是如果我在 MyClass 中更改其中一个测量的属性,则使计算器 class 触发 OnPropertyChanged 方法。我怎样才能将信息基本上从一个 child 传递到另一个 child 的 child?当我使用 Evaluators 的 DataGrid 时,例如单击 header 重新排列它,它起作用了。所以我猜问题是 C# 代码而不是 xaml。所以我暂时不提它。所有绑定都是 Mode=TwoWay(bool 除外,因为它没有 setter)和 UpdateSourceTrigger=PropertyChanged.

我试图勾勒出问题所在:

这是我的代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Text;

namespace demo
{
    public class MyClass : INotifyPropertyChanged
    {
        public class Measurement : INotifyPropertyChanged
        {
            private double? myValue1;

            public double? MyValue1
            {
                get { return myValue1; }
                set
                {
                    myValue1 = value;
                    OnPropertyChanged("MyValue1");
                }
            }

            private double? myValue2;

            public double? MyValue2
            {
                get { return myValue2; }
                set
                {
                    myValue2 = value;
                    OnPropertyChanged("MyValue2");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }

        }

        public class EvaluationRule
        {
            public EvaluationRule(double Value1Min, double Value2Min)
            {
                this.Value1Min = Value1Min;
                this.Value2Min = Value2Min;
            }

            public double Value1Min;
            public double Value2Min;

        }

        public class Evaluator : INotifyPropertyChanged
        {
            public Evaluator(Measurement Measurement, EvaluationRule Rule)
            {
                this.Rule = Rule;
                this.Measurement = Measurement;
            }

            public EvaluationRule Rule;

            private Measurement measurement;
            public Measurement Measurement
            {
                get { return measurement; }
                set
                {
                    measurement = value;
                    OnPropertyChanged("Measurement");
                }
            }

            public bool IsApproved
            {
                get
                {
                    if (measurement.MyValue1 > Rule.Value1Min
                        && measurement.MyValue2 > Rule.Value2Min)
                    {
                        return true;
                    }
                    return false;
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public class CollectionOfEvaluators : INotifyPropertyChanged
        {
            public CollectionOfEvaluators(EvaluationRule Rule, ObservableCollection<Measurement> Measurements)
            {
                this.Rule = Rule;
                this.Measurements = Measurements;

                var Evaluators = new ObservableCollection<Evaluator>();
                foreach (var _measurement in Measurements)
                {
                    var _evaluator = new Evaluator(_measurement, this.Rule);
                    Evaluators.Add(_evaluator);
                }
            }

            public EvaluationRule Rule;

            private ObservableCollection<Measurement> measurements;

            public ObservableCollection<Measurement> Measurements
            {
                get { return measurements; }
                set
                {
                    measurements = value;
                    OnPropertyChanged("Measurements");
                }
            }

            private ObservableCollection<Evaluator> evaluators;

            public ObservableCollection<Evaluator> Evaluators
            {
                get { return evaluators; }
                set
                {
                    evaluators = value;
                    OnPropertyChanged("Evaluators");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;

            private void OnPropertyChanged(string propertyName)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private ObservableCollection<Measurement> measurements;

        public ObservableCollection<Measurement> Measurements
        {
            get { return measurements; }
            set
            {
                measurements = value;
                OnPropertyChanged("Measurements");
            }
        }

        private ObservableCollection<EvaluationRule> rules;

        public ObservableCollection<EvaluationRule> Rules
        {
            get { return rules; }
            set
            {
                rules = value;
                GetCollection();
            }
        }

        private ObservableCollection<CollectionOfEvaluators> collection;

        public ObservableCollection<CollectionOfEvaluators> Collection
        {
            get { return collection; }
            set
            {
                collection = value;
                OnPropertyChanged("Collection");
            }
        }

        public void GetCollection()
        {
            var Collection = new ObservableCollection<CollectionOfEvaluators>();
            foreach (var _rule in rules)
            {
                var _collection = new CollectionOfEvaluators(_rule, Measurements);
                Collection.Add(_collection);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

您必须委托该活动。 Evaluator 应该监听其聚合 MeasurementPropertyChanged 事件。然后此事件的处理程序可以引发 Evaluator.PropertyChanged 事件作为响应:

public class Evaluator : INotifyPropertyChanged
{
  public Evaluator(Measurement measurement, EvaluationRule rule)
  {
    this.Rule = rule;
    this.Measurement = measurement;

    this.Measurement.PropertyChanged += OnMeasurementPropertyChanged;
  }

  public void OnMeasurementPropertyChanged(object sender, PropertyChangedEventAgrs e)
  {
    OnPropertyChanged(nameof(this.Measurement));
  }

  private Measurement measurement;
  public Measurement Measurement
  {
    get  => this.measurement
    set
    {
      this.measurement = value;
      OnPropertyChanged(nameof(this.Measurement));
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;
  private void OnPropertyChanged(string propertyName)
  {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  }
}

请注意,您在命名 class 时遇到了拼写错误。是我a肯定 - 你错过了 'a'。此外,参数名称应始终为小写。