开发者

WPF: What are the advantages/disadvantages of using WPF UserControl? [closed]

开发者 https://www.devze.com 2023-02-20 18:21 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the 开发者_开发技巧help center for guidance. Closed 10 years ago.

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消