0

I'm having a problem concerning an ObservableCollection that is nested in a class.

Let's first start with the class:

public class Range : ObservableObject
{

    public VSE100_RPM_Range()
    {
        MyLimit = new ObservableCollection<Limit>();
        MyLimit.CollectionChanged += OnCollectionChanged;
    }

    public void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            foreach (Limit item in e.NewItems)
            {
                item.LowerLimit = item.UpperLimit - Range_Gap;
            }
            RegisterPropertyChanged(e.NewItems);
        }
        else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            UnRegisterPropertyChanged(e.OldItems);
        }
        else if (e.Action == NotifyCollectionChangedAction.Replace)
        {
            UnRegisterPropertyChanged(e.OldItems);
            RegisterPropertyChanged(e.NewItems);
        }
        else if (e.Action == NotifyCollectionChangedAction.Reset)
        {
            foreach (Limit item in e.NewItems)
            {
                item.LowerLimit = item.UpperLimit - Range_Gap;
            }
        }
    }

    void RegisterPropertyChanged(IList items)
    {
        foreach (INotifyPropertyChanged item in items)
        {
            if (item != null)
            {
                item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    void UnRegisterPropertyChanged(IList items)
    {
        foreach (INotifyPropertyChanged item in items)
        {
            if (item != null)
            {
                item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
            }
        }
    }

    void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        OnCollectionChanged(sender, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
    }

    public const string Range_GapPropertyName = "Range_Gap";
    private float _range_gap;
    public float Range_Gap
    {
        get
        {
            return _range_gap;
        }

        set
        {
            if (_range_gap == value)
            {
                return;
            }

            _range_gap = value;
            item_PropertyChanged(Range_Gap, new PropertyChangedEventArgs(Range_GapPropertyName));
            RaisePropertyChanged(Range_GapPropertyName);
        }
    }


   
    public ObservableCollection<Limit> MyLimit { get; set; }
}

public class Limit: ObservableObject
{           
    public const string UpperLimitPropertyName = "UpperLimit";
    private float _upperlimit;
    public float UpperLimit
    {
        get
        {
            return _upperlimit;
        }

        set
        {
            if (_upperlimit == value)
            {
                return;
            }

            _upperlimit = value;
            RaisePropertyChanged(UpperLimitPropertyName);
        }
    }


    public const string LowerLimitPropertyName = "LowerLimit";
    private float _lowerlimit;
    public float LowerLimit
    {
        get
        {
            return _lowerlimit ;
        }

        set
        {
            if (_lowerlimit == value)
            {
                return;
            }

            _lowerlimit = value;
        }
    }
}

(I'm using MVVMLight, this explains the usage of ObservableObject and RaisePropertyChanged.)

I'm editing the UpperLimit property via a UI and calculate the LowerLimit according to the code. This works so far.

Now what I'm trying to do is to fire a CollectionChanged event when the Range_Gap property is changed. In this case I want to update the whole collection and execute the calculation. This works too, but it is not displayed on the UI.

Can you help me to solve this problem or point a better solution out?

Thank you very much.

Philipp

0 Answers0