I’m trying to create a MindMap programm using WPF with MVVM.
I have a Model called “NodeModel” that has a list of Child NodeModels.
public class NodeModel
{
public string Content { get; set; }
public Vector2 Position { get; set; }
public NodeModel ?ParentNode { get; set; }
public List<NodeModel> ChildrenNodes { get; }
}
Now I wanted to implement the NodeViewModel, so the view can be notified to any changes made by the Node.
public class NodeViewModel : ViewModelBase
{
private string _content;
public string Content
{
get
{
return _content;
}
set
{
_content = value;
OnPropertyChanged(nameof(Content));
}
}
private Vector2 _position;
public Vector2 Position
{
get
{
return _position;
}
set
{
_position = value;
OnPropertyChanged(nameof(Position));
}
}
private NodeModel? _parentNode;
public NodeModel ParentNode
{
get
{
return _parentNode;
}
set
{
_parentNode = value;
OnPropertyChanged(nameof(ParentNode));
}
}
public NodeViewModel(NodeModel node)
{
Content = node.Content;
Position = node.Position;
ParentNode = node.ParentNode;
}
}
The Problem I now have is that I don’t know how I should handle the list I made in NodeModel in the ViewModel. I cannot just use NodeModel List, because then the NodeModels cannot notify the view. But I cannot use the NodeViewModels because my List only supports NodeModels. What should I do?
My solution is to just change the list type in the NodeModel to NodeViewModel. But it doesn’t feel right to do so, because then the Models aren’t properly encapsulated from the ViewModel.
user26135571 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.