Where to put property change callback code so I can handle the previous value of the property without using something different than BindableBase?

by silviubogan   Last Updated October 09, 2019 14:05 PM

I have this uncompilable code below, it uses the code in this answer. In it gv means "the old value of a group's name", where a group is a group of clocks, and a clock is a timer or alarm. It practically synchronizes existing items in two ObservableCollections of different but related item types that should not necessary know of each other.

I want to know what are the best structural changes I can make so I can use the gv variable as being the old value of the changed property. I would also like an article or a book recommendation which learns me this way of thinking in C# or similar programming languages.

I can post any code part as necessary. Here are all the files I found relevant to the question. They contain the following classes:

Here is my previous question on this website (this is somehow its continuation), it has other files and a code snippet too there.

Code snippet from the class FilterMCollection

public void Initialize()
{
    l1 = OcPropertyChangedListener.Create(MyClocks.Filters.Ms);
    l1.PropertyChanged += l1_PropertyChanged;
    l2 = OcPropertyChangedListener.Create(MyClocks.Groups.Ms);
    l2.PropertyChanged += l2_PropertyChanged;
}
private void l2_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    var g = sender as ClockGroupM;
    foreach (FilterM f in MyClocks.Filters.Ms)
    {
        if (!f.IsBaseFilter && g.Name == f.GroupNames.FirstOrDefault())
        {
            f.DisplayString = g.Name;
            f.IsSelected = g.IsSelected;
            f.IsBaseFilter = false;
            f.MyConstantImageSource = g.Icon;
            f.MyEmptyImageSource = MyClocks.FiltersVM.MyEmptyFolderIcon;
            f.MyNonEmptyImageSource = MyClocks.FiltersVM.MyNonEmptyFolderIcon;
            f.GroupNames.Clear();
            f.GroupNames.Add(g.Name);
            f.UpdateItemCount();
        }
        else
        {
            if (!f.GroupNames.Contains(gv))
            {
                continue;
            }
            var ng = new ObservableCollection<string>(f.GroupNames);
            ng.Remove(gv);
            ng.Add(g.Name);
            f.GroupNames = ng;
            f.DisplayString = g.Name;
        }
    }
}
private void l1_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    var f = sender as FilterM;
    if (f.GroupNames.Count == 0 || f.GroupNames.Count > 1)
    {
        return;
    }
    foreach (ClockGroupM g in MyClocks.Groups.Ms)
    {
        if (g.Name == f.GroupNames.FirstOrDefault())
        {
            g.Name = f.DisplayString;
            g.Icon = f.MyConstantImageSource;
            g.IsSelected = f.IsSelected;
            break;
        }
    }
}

Code snippet from the class ClockGroupMCollection

Here is the Rename method that changes the property Name of a ClockGroupM:

internal bool Rename(string g, string gg)
{
    if (string.IsNullOrEmpty(g) ||
        string.IsNullOrEmpty(gg) ||
        Contains(gg))
    {
        return false;
    }
    bool changed = false;
    for (int i = 0; i < Ms.Count; ++i)
    {
        if (Ms[i].Name == g)
        {
            Ms[i].Name = gg;
            changed = true;
            break;
        }
    }
    if (changed)
    {
        foreach (ClockM td in Clocks.Ms)
        {
            if (td.GroupName == g)
            {
                td.GroupName = gg;
            }
        }
        return true;
    }
    return false;
}

I've thought about moving some code from the l1_PropertyChanged and l2_PropertyChanged handlers into the Rename method but I do not think this is good design.

Note: BindableBase is Prism's implementation of INotifyPropertyChanged. And I know that there are multiple possible answers to my question, I wish to know the correct one which is as good as possible on the long term. My application is currently a clock/time manager (it has timers and alarms, for example, and they can be grouped, searched etc.).

Adding to the actual question in the title, I would preffer to not replace BindableBase, but if it is not possible without this replacement, should I use inheritance from DependencyObject or to use this?

Thank you.



Related Questions


Updated November 23, 2017 17:05 PM

Updated March 19, 2017 07:05 AM

Updated July 10, 2017 05:05 AM

Updated July 18, 2015 13:02 PM

Updated September 27, 2017 21:05 PM