-1

In my WPF application, I want to build a graph where I sum up a "measured" current and display this for every graph element.

I tried using the MVVM Toolkit for this and set up 2 classes:

    public partial class CurrentFlow:ObservableObject
    {
        [ObservableProperty]
        private int _id;
        [ObservableProperty]
        private float _current;
    }
    public partial class Connection:CurrentFlow
    {
        [ObservableProperty]
        private CurrentFlow? _inflow1;

        [ObservableProperty]
        private CurrentFlow? _inflow2;

        
        partial void OnInflow1Changed(CurrentFlow? value)
        {
            Current = value?.Current ?? 0.0f + Inflow2?.Current ?? 0.0f;
        }

        partial void OnInflow2Changed(CurrentFlow? value)
        {
            Current = value?.Current ?? 0.0f + Inflow1?.Current ?? 0.0f;
        }
    }

I then create the graph like this, where Powerstages is an ObservableCollection<CurrentFlow> and TerminalConnection is a Connection:

        private void CreateConnections()
        {
            var lastConnection = TerminalConnection;
            foreach (var ps in Powerstages)
            {
                lastConnection.Inflow1 = ps;

                if (ps != Powerstages.LastOrDefault())
                {
                    var con = new Connection { Id = ps.Id + 100000 };
                    lastConnection.Inflow2 = con;
                    lastConnection = con;
                }
            }
        }

My problem is, that the OnInflow1Changed and OnInflow2Changed methods don't get triggered when I change the Powerstages[i].Current property.

Has anyone an idea what I am doing wrong and where I can improve?

I suspect the PropertyChanged event does not propagate, but I thought this would be the exact usecase for the [ObservableProperty]attribute.

I could manually subscribe to the currents change events, but Im hoping there is a neat way using the MVVM Toolkit.

3
  • So Current is basically calculated as Inflow1 + Inflow2. Why is it then even writeable? What would you do it it is changed from outside? Commented Apr 15 at 15:33
  • Good point. I need it to be writable on the CurrentFlow class, but not in the Connection class anymore. Commented Apr 15 at 16:26
  • 1
    Powerstages[i] is a CurrentFlow but not a Connection. Why do you expect changing the property of the former calls the methods of the latter? Commented Apr 15 at 20:36

1 Answer 1

0

I'm not entirely sure if I understood you correctly.
But take a look at this solution. Maybe it's what you need.

    public partial class CurrentFlow : ObservableObject
    {
        [ObservableProperty]
        private int _id;
        [ObservableProperty]
        private float _current;
    }
    public partial class Connection : ObservableObject /*: CurrentFlow*/
    {
        [ObservableProperty]
        private CurrentFlow? _inflow;

        [ObservableProperty]
        private Connection? _previous;


        public float Total { get; private set; }
        private static readonly PropertyChangedEventArgs totalChangedEventArgs = new PropertyChangedEventArgs(nameof(Total));

        partial void OnInflowChanged(CurrentFlow? oldValue, CurrentFlow? newValue)
            => OnPropChanged(oldValue, newValue);
        partial void OnPreviousChanged(Connection? oldValue, Connection? newValue)
            => OnPropChanged(oldValue, newValue);

        private void OnPropChanged(INotifyPropertyChanged? oldValue, INotifyPropertyChanged? newValue)
        {
            if (oldValue is not null)
            {
                oldValue.PropertyChanged -= OnCurrentChanged;
            }
            if (newValue is not null)
            {
                newValue.PropertyChanged += OnCurrentChanged;
            }
            OnCurrentChanged(null, null!);
        }

        private void OnCurrentChanged(object? sender, PropertyChangedEventArgs e)
        {
            Total = Inflow?.Current ?? 0.0f + Previous?.Total ?? 0.0f;
            OnPropertyChanged(totalChangedEventArgs);
        }

    }
        private void CreateConnections()
        {
            var lastConnection = TerminalConnection;
            for (var i = 0; i < Powerstages.Count; i++)
            {
                lastConnection.Inflow = Powerstages[i];
                lastConnection = new Connection() { Previous = lastConnection};
            }
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.