Avoid ObservableCollection in MVVM Model

Viewed 4731

I have a Model class with a collection and a ViewModel wrapper class for that Model class. The wrapper class implements INotifyPropertyChanged and has a wrapper property for every property in the model class. I've implemented it this way to make the model class as independent from any WPF namespace as possible, because these classes are also used in another project (a windows service). The implementation (simplified) looks like this:

Model

public class FbiDirectory
{
    private string type;
    private ObservableCollection<PluginValue> pluginValues = new ObservableCollection<PluginValue>();

    public string Type
    {
        get
        {
            return this.type;
        }

        set
        {
            this.type = value;
        }
    }

    public ObservableCollection<PluginValue> PluginValues
    {
        get
        {
            return this.pluginValues;
        }

        set
        {
            this.pluginValues = value;
        }
    }
}

ViewModel wrapper

public class FbiDirectoryViewModel : INotifyPropertyChanged
{
    private FbiDirectory fbiDirectory = new FbiDirectory();

    public string Type
    {
        get
        {
            return this.fbiDirectory.Type;
        }

        set
        {
            this.fbiDirectory.Type = value;

            this.OnPropertyChanged("Type");
            this.OnPropertyChanged("Title");
        }
    }

    public ObservableCollection<PluginValue> PluginValues
    {
        get
        {
            return this.fbiDirectory.PluginValues;
        }

        set
        {
            this.fbiDirectory.PluginValues = value;

            this.OnPropertyChanged("PluginValues");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

My question is if there is a way to make the PluginValues collection in the Model of type List and still have the features of ObservableCollection in the ViewModel. Perhaps with some sort of converter or casting or anything like this.

3 Answers
Related