-1

I am completely new to WPF and MVVM, that's why I decided to start working on that. I am developing a Battleship game. If I create Canvas in my View, and create another canvas in ViewModel and bind them - everything works fine. However there is a problem. To begin with, I add Rectangles and TextBlocks to my Canvas to represent the grid. My "Field" Class (Model) is used in ViewModel for the logic to be done. And I want to have the property inside that class IsLegendHidden (bool). If I set that to true, then the method is called. In that method the linq looks for the textblocks related to my legend and then sets their visibility to hidden. It works fine, however having logic in the Model class is wrong in MVVM structure. But moving the algorithm to the ViewModel side will eliminate the ability to use class property "IsLegendHidden". But I want to have that property inside the model class. How can I solve that?

pro100tom
  • 165
  • 1
  • 10
  • if you are new to wpf and new to mvvm you should just try to learn them one at a time not both together – ZoomVirus Jan 09 '15 at 15:55
  • This is not the answer to the question, however you are right, I should start learning WPF first. But I have experience with Windows Forms and HTML, I understood how bindings work and was able to build simple application with binded textboxes and button action. But the question remains. – pro100tom Jan 09 '15 at 15:58
  • It can be difficult to jump into WPF knowing something like Windows Forms. It's almost worse because your knowledge ends up being counter productive. I think if you focus on MVVM stuff you'll be able to write simple WPF programs. – KirkSpaziani Jan 09 '15 at 16:02

1 Answers1

1

Your Model Class is free to implement INotifyPropertyChanged and it can notify the ViewModel when a particular property changes. This allows you to have a simpler model and get the logic done in the ViewModel.

You can also have a separate modeling of properties that are more focused on view concerns that the ViewModel can consult.

When using MVVM I like to try to separate things as much as possible. The model objects should just represent the concept they're modeling. The ViewModel is tied to a specific view so it will interpret or translate the model's properties into what is important to the view. You can have something on the Model that isn't 'IsLegendHidden', but that powers the ViewModel's 'IsLegendHidden' property.

KirkSpaziani
  • 1,962
  • 12
  • 14
  • Battleship case: 1). we create a "GameField" class with many fields including "Canvas" and "IsLegendHidden" 2). we use our "Window" class as a view and add there a "Canvas" control 3). we create a "WindowViewModel" class, create "GameField" instance in it and bind the whole "Canvas" with the "GameField" field "Canvas".In my "WindowViewModel" class I add "rectangles" representing grid and "textblocks" representing legend to my canvas like myCanvas.Children.Add(). I want to say "IsLegendHidden=true" so that Visibility property of all "textblocks" representing the legend would be set to "hidden". – pro100tom Jan 09 '15 at 16:37
  • When you say Canvas do you mean the WPF Canvas class or something else? A WPF Canvas should only be in the view and you can bind its Children. Take a look at this question: http://stackoverflow.com/questions/2317713/binding-wpf-canvas-children-to-an-observablecollection That might give a way to do what you want. – KirkSpaziani Jan 09 '15 at 18:01
  • Thank you very much! I will check it out. I actually use System.Windows.Controls.Canvas. Basically I want to change the color of the rectangles on canvas. And I have an algorithm for that. Is it ok if the algorithm is in view class? – pro100tom Jan 09 '15 at 18:59
  • Typically you want to avoid logic in the view class. You might have something like: a Rectangle is a view. There's a RectangleViewModel that has a notifying color property the rectangle's color is bound to. The owner of the RectangleViewModel (likely another viewmodel) would set those properties using the algorithm. Your View then would be bound to the collection of RectangleViewModels... – KirkSpaziani Jan 09 '15 at 19:03
  • This: http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode Is a very good article that worth going through. It teaches how to build an MVVMy TreeView in WPF. Once you get those techniques mastered the problem you're working on should be easier to solve. – KirkSpaziani Jan 09 '15 at 19:08
  • But doesn't it break the encapsulation of the class "GameField"? When I used windows forms, I create a class with all properties and methods with different access levels. I was hoping to create a field already with the rectangles inside it. If I have a class "RectangleViewModel" it will reveal some of the stuff from my "GameField". – pro100tom Jan 09 '15 at 19:14
  • Think more about modular encapsulation vs. a single class. Here a bunch of objects are going to work together to solve your problem. The data won't leak outside of that. Also maybe think about it in a different way. Maybe your model has that algorithm. It is the representation of all these objects - the view 'chooses' to render them as rectangles. The model can have a set of properties with each one set by the algorithm that the viewmodel translates into a color for the view. – KirkSpaziani Jan 09 '15 at 19:18
  • So it is all right if the model has algorithms or only the ViewModel should have them implemented? I am also confused about the scope. With forms I know: here's the main class and I use other classes when needed. But here we have both the View, and ViewModel. The person who is working on the view should know nothing about ViewModel, right? – pro100tom Jan 09 '15 at 19:34
  • I consider the XAML part of the "view" layer. From the perspective of your XAML, it has a "DataContext" which is the abstract way of referring to your ViewModel - All bindings start relative to the DataContext. So the XAML does need to know about the public properties exposed by the ViewModel – KirkSpaziani Jan 09 '15 at 20:20