Bear with me because I’m new to WPF/XAML. I’m trying to write a MainWindow.xaml / App.xaml pair that properly isolates stylization and data binding, as close to a model-view-controller as possible. So far:
- I have a styles.xaml that contains all of the formatting (fonts, margins, etc.). It’s referred to from app.xaml’s Application.Resources so that it can apply to all WPF forms in the project.
- Model data collections exist as member variables of App, and App exposes them via GetX() acccessor functions.
-
mainwindow.xaml contains the view structure, as well as binding information. This is done like so:
ObjectDataProvider x:Key=”AppX” MethodName=”GetX”
ObjectInstance=”{x:Static local:App.Current}”
The fact that mainwindow.xaml contains binding information is bad, since that should be the role of the controller (app). Unfortunately, I’ve found out that one of the restrictions of the WPF designer is that you can’t (for instance) have two unkeyed Resources references (one for view styles, one for data binding styles). Similarly, app.xaml can’t contain both the styles.xaml reference and a set of data binding styles in its Resources section.
I could theoretically manually instantiate the main window from the app’s startup handler, and then do the binding in code, but that would violate the spirit of WPF in two ways – the app shouldn’t need to make the window, as that’s done in the background; and binding is better done in XAML than in code when possible. If there were a way for the app to be notified that the main window has been created so that it can then do binding, that wouldn’t be bad, but I haven’t seen any (short of the main window calling the app after load, but that shouldn’t be the responsibility of the view).
So I’m kind of out on a limb. I don’t know of a way to properly adhere to MVC while still doing things “the WPF way” – it seems that the default is for the VS designer to create a monolithic window XAML that has a mishmash of view, view stylization, and controller. Any descriptions on how to do this better or links to good websites on the topic will be appreciated.
MVVM is essentially MVC. In MVVM there is an extra seperation of the View into a view and view model where the view now is merely the controls of the UI and the view model holds the data and logic of the view. Additionaly, in MVVM, the relationship between the View and View Model, in isolation, can also be seen as the MVC pattern where M = ViewModel, V = View, C = DataBinding.