How do I create a reusable custom control in a custom control in WPF?
I am using WPF with WAF map.
I have a piece of UI (let it collect user credentials) that is displayed in multiple views (see image).
I figured this is reusable, let it fit in its own custom control.
I can get everything to work just fine if I ignore the inner user control and just "flatten" it if you do, but trying to encapsulate it makes me wonder what is the best approach. Should this "credential" user control have its own dedicated view model? Should it publish its data via dependency properties? What's the best approach?
I will need to output the data collected from the credential control to the view model of the external user control.
source to share
Should this "credential" user control have its own dedicated view model? Should it publish its data via dependency properties? What's the best approach?
UserControl user can perform two tasks -
If you want to use it as a "Control", which is often the case in this situation, I would consider it as a 100% representation. This way I would not make a UserControl a ViewModel at all (at least not publicly) and expose its properties via Dependency Properties. This provides the most flexibility in terms of reuse, as the UserControl acts like any other FrameworkElement and can be dropped and bound to your own properties in your other location just like any other control.
When the UserControl acts as a view for the ViewModel, however, things are different. The goal here is not reuse, but separation of concerns between your view and the virtual machine.
This sounds more like the first situation - you want a control that can be reused in multiple places. In this case, it basically becomes a view element.
source to share