I was looking at creating a common control that I will be able to reuse on my pages: an AddressControl which has Address1, Address2, City, State, Zip, etc...
Originally I just created a class (AddressEntity
) that contained all these items and implemented INotifyPropertyChanged
. I included that class as a DependencyProperty in my Code-Behind for the AddressControl and used it as the DataContext for the bindings to its properties.
Then, someone said my code was ugly and I should look into MVVM. Looking at it, I assume that:
The problem is every example I've ever seen has a UserControl as the View and not a CustomControl. Before I delve too deep into this...
References: The Model-View-ViewModel (MVVM) Design Pattern for WPF
The Windows Presentation Framework (WPF) takes full advantage of the Model-View-ViewModel (MVVM) pattern. Though it is possible to create WPF applications without using the MVVM pattern, a little investment in learning can make building WPF applications much simpler.
In MVC, controller is the entry point to the Application, while in MVVM, the view is the entry point to the Application.
MVVM (Model-View-ViewModel) MVVM is a way of creating client applications that leverages core features of the WPF platform, allows for simple unit testing of application functionality, and helps developers and designers work together with less technical difficulties.
CustomControls are never done with mvvm.
What you want is a reusable view(user control) of your data and not a control(custom control).
UserControls and CustomControls are two completely different beasts.
EDIT:
Notwithstanding why UserControls were originally developed, in MVVM typically you use a UserControl when you want a reuseable view which is specific to your model/viewmodel. Its just XAMl without any code behind (except for the auto generated InitializeComponent stuff). Generally you keep a UserControl in the same project that you use it in.
You go for a CustomControl when you want a generic piece of functionality which requires a view and which has potential use even outside the scope of your current application. Here the control is actually defined in a code file and the look (which can be overriden) comes via XAML in a resource dictionary. Generally you keep a CustomControl in a a seperate ControlLibrary project and reference the library in the project you wish to use it in.
With due respect to WallStreetProgrammer, choosing between a user control and a custom control based solely on whether or not you want a lookless control is a bit naive.
When using MVVM the Model and ViewModel should not be dependent on the View, that is they should not care what kind of view use them.
The difference between a custom control and a usercontrol in WPF is that the custom control is lookless and can be customized via its ControlTemplate. This is what you should write if you are writing a generic control library, like Microsoft does. If you however have a specific look in mind for you control, just go with a user control, it is much faster but will only have one look, the one you define for it.
It is common to use a mix of custom controls and user controls in a MVVM project. For example you would probably use a bunch of custom controls from Microsoft (like textboxes and textblocks) and combine them into user controls.
See Control Authoring Overview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With