What to expand for a new user control?

Some people suggest simply expanding the Panel , but I also know about UserControl , which sounds like something that I have to expand to make my own control.

Panel vs UserControl vs Control vs another_alternative

 // inheritance depth: Control Panel : ScrollableControl : Control UserControl : ContainerControl : ScrollableControl : Control 

I need a rectangle for drawing and the full ability of WinApi (Handle, WndProc, etc.). But I want to avoid all the add-ons that may arise in the game. I want to use my own controls to add them to standard forms. No extreme action.

UserControl, on the other hand, extends ContainerControl, which may have the functionality I want (I'm not sure).


I inherited from Control for the OpenGL window and some other simple controls. While they are working fine. I did not notice any functional loss. Does something speak against Control ? (if I do not require additional features)
+4
source share
4 answers

I think that the informal and accepted "standard" should simply derive from UserControl . At least this is what I saw everywhere. It does not do it right!

On the other hand, I think you can also worry too much about the β€œright thing” here. For me, this is almost like premature optimization. You want you to spend your resources wisely.

Also keep in mind that such things can always be changed later.

If you are worried for some reason, I would definitely highlight the simplest controls with lower inheritance.

+4
source

Basically, UserControl is the best class to inherit from. Another option might be to inherit from the class we want to configure.

In your case, Panel should be the best class for inheritance, since it is the simplest management class that will meet your requirements.

0
source

Panel will be ideal here because you are not defining a layout for the control. Also, no ControlTemplate is required. Custom rendering schemes typically use the recommendation to use Panel . Although Control is the simplest "layout" element you can get, Panel has the ability to place child controls if you need to later. Fine!

0
source

There is a description of what exactly distinguishes UserControl from ContentControl in Pro WPF, which provides more information than MSDN. Here is a direct link to the section:

http://books.google.ca/books?id=znAVMHNSen0C&pg=PA518&lpg=PA518&dq=%22taking ++ = 0CC8Q6AEwAQ # v = onepage & q =% 22taking% 20a% 20closer% 20look% 20at% 20user% 20controls% 22 & f = false

The most interesting difference is how it handles routed events. All events caused by the contained elements have a Source reset for the UserControl itself.

I think the conceptual difference is that ContentControl "contains" its content, while UserControl content is a "part" of UserControl. Thus, ContentControl is a separate element (custom / tab tab), the contents of which are processed separately; and customizing the contents of UserControl is part of its design process, the elements of which are processed directly in the code of the UserControl code.

So my rules are:

  • If the user should inherit it, and handle content in the code, inherit UserControl .
  • If it is intended to use as-is and arbitrary content is specified, inherit the ContentControl .
  • If it defines its own content and does not have a placeholder for arbitrary content, inherit Control and set its Template .

If you just want a surface rendering, I don’t think you will need Control . You can use an ImageSource for this, such as InteropBitmap or D3DImage , which you can bind to ImageBrush . For example, it can be used in a separate rectangle:

 <Rectangle> <Rectangle.Fill> <ImageBrush> <ImageBrush.ImageSource> <!-- ImageSource here --> </ImageBrush.ImageSource> </ImageBrush> </Rectangle.Fill> <Rectangle> 

Or in another place that Brush accepts, including Background another control.

0
source

Source: https://habr.com/ru/post/1496535/


All Articles