WPF: What are the advantages/disadvantages of using WPF UserControl? [closed]
I just want to raise this question about WPF's User Control. I just started using this 'control' in my WPF applications, instead of 'adding' all my controls in one WPF window.
Anyways, would I get a faster process time if I use User Control? How about Dependency Properties? Can I still 'communicate' between my User Control and my WPF window?
What are your thoughts about it? Thanks in advance.
UserControls should be used for reuse, but this is not the main reason to use them.
the main reason to use useControsl is to seperate different parts of your xaml, so that each part is in charge of one responsibility only. for example, if you would write VisualStudio, you would create the ToolBox in one userControl, the CodeEditor in another userControl, etc. this is mainly for SRP, or Single Responsibility Priniciple.
Reuse, or DRY (Don't Repeat Yourself) is important, but SRP is WAY more important.
As to communication - yes, you would use dependecy properties.
The main reason to use UserControls is to bundle a set of controls so they can be reused (normally within the same application)
As for 'performance' you are adding an extra node to the visual tree so it would be slower but that will be hardly noticeable.
Communication between a UserControl and the window (or its parent) is completely similar to any other control you add to a layout class.
I only use UserControl
when it's need to share group of controls between two or more places. To communicate between UserControl
and Window
you should implement DependencyProperty
-ies in your user control. It's disadvantage. So, when it's possible I avoid using UserControl
and use it only, for instance, to share layout between two versions (WPF
and XBAP
).
精彩评论