locked
Creating Custom- / User-Controls - best practice? RRS feed

  • Question

  • I'm developing an app where i have alot of controls but until now i've only used Canvas as baseclass wich works fine. However i'm getting more and more interested in the Template-Property in the Control so i'm thinking about converting all my controls to a UserControl or CustomControl.

    But i don't know what's the difference between them and how to use them.

    Using a Canvas as baseclass was very easy becouse it's pretty similar to the old WinForm-UserControls where you just place out all your children in the baseclass. UserControls/CustomControl doesn't event have any Children-property, it seems like everything is stored in the ResourceDictionaries. How should i do to dynamicly populate the content of these controls?

    I want to know everything about creating your own Controls by using Custom/UserControl or any other baseclass that might fit even better.

     

    I'm also wondering when the visual designer will get support for custom controls and resourcedictionaries?

    Monday, September 25, 2006 4:13 PM

Answers

  • Chimme-

    Footballism had a great reply.

    Let me add a few things:

    • Moving from a Canvas based world to a UserControl should be easy.  UserControl has a Content property, just like Window/Page.  Just like Window and Page, generally a Panel (Grid, Canvas, WrapPanel, StackPanel, etc...) will be the only child of UserControl.
    • The key thing about UserControl based development, like Window and Page, is that you can Name elements and code to them from code behind.  So if you say you want to dynamically populate content of those controls, you can do that.

    Generally, you should ask about the Visual Designer for custom controls and resource dictionaries on the "Cider" forum and/or Expression Interactive Designer forum.  I will try to answer the little I can in that area:

    • I believe you can host Custom Controls in Cider if you follow the steps they give you...(they must be in a seperately assembly, they can't be project references, etc...)   They plan on fixing this, but it may need to wait for some "AppDomain" hosting work they can't do on top of VS 2005.  It may wait until Cider is shipping with the real "Orcas" CTPs -- meaning not the Orcas preview on top of VS 2005.

    Generally, you should start with UserControl unless you need the power/flexibility of CustomControls for some reason.  To learn more about CustomControls, today I found a nice post by Pavin Podila about building WPF Custom Controls.

    Hope that helps.

    Thanks, Rob Relyea
    Program Manager, WPF Team
    http://rrelyea.spaces.live.com

    Monday, September 25, 2006 6:49 PM
  • Well the difference between user control and custom control is that If you have a portion of views or UI which will be used repeatedly in your own project, using UserControl is a much simpler and preferred way, but if you have a portion of UI and functionality which you believe will be used aross different projects, then using custom control is the right approach.
    And another difference between user control and custom control is that when using user control, you can simply add your views into the UserControl's Content, since UserControl is derived from ContentControl, so you are safe to do so, and this approach I believe is what user control designed for. but as custom control, you should define the views inside it's ControlTemplate, you define the templates and styles on one hand, and write code which will interacts with the templates on the other hand, and what you defined for styles and templates will be used as the default look and feel of your custom control. so the user of your custom control are free to override this theme style, but the user has to obey the "logic" you create for the templates.

    Sheva
    Monday, September 25, 2006 4:48 PM

All replies

  • Well the difference between user control and custom control is that If you have a portion of views or UI which will be used repeatedly in your own project, using UserControl is a much simpler and preferred way, but if you have a portion of UI and functionality which you believe will be used aross different projects, then using custom control is the right approach.
    And another difference between user control and custom control is that when using user control, you can simply add your views into the UserControl's Content, since UserControl is derived from ContentControl, so you are safe to do so, and this approach I believe is what user control designed for. but as custom control, you should define the views inside it's ControlTemplate, you define the templates and styles on one hand, and write code which will interacts with the templates on the other hand, and what you defined for styles and templates will be used as the default look and feel of your custom control. so the user of your custom control are free to override this theme style, but the user has to obey the "logic" you create for the templates.

    Sheva
    Monday, September 25, 2006 4:48 PM
  • Chimme-

    Footballism had a great reply.

    Let me add a few things:

    • Moving from a Canvas based world to a UserControl should be easy.  UserControl has a Content property, just like Window/Page.  Just like Window and Page, generally a Panel (Grid, Canvas, WrapPanel, StackPanel, etc...) will be the only child of UserControl.
    • The key thing about UserControl based development, like Window and Page, is that you can Name elements and code to them from code behind.  So if you say you want to dynamically populate content of those controls, you can do that.

    Generally, you should ask about the Visual Designer for custom controls and resource dictionaries on the "Cider" forum and/or Expression Interactive Designer forum.  I will try to answer the little I can in that area:

    • I believe you can host Custom Controls in Cider if you follow the steps they give you...(they must be in a seperately assembly, they can't be project references, etc...)   They plan on fixing this, but it may need to wait for some "AppDomain" hosting work they can't do on top of VS 2005.  It may wait until Cider is shipping with the real "Orcas" CTPs -- meaning not the Orcas preview on top of VS 2005.

    Generally, you should start with UserControl unless you need the power/flexibility of CustomControls for some reason.  To learn more about CustomControls, today I found a nice post by Pavin Podila about building WPF Custom Controls.

    Hope that helps.

    Thanks, Rob Relyea
    Program Manager, WPF Team
    http://rrelyea.spaces.live.com

    Monday, September 25, 2006 6:49 PM
  • Thanks, i think i at least understand the basics of it now.

    But is it possible to dynamicly populate a CustomControl? I have a set of controls that includes a custom Button and a custom ListView, i can't use the ones that come with the framework because my program requires a special focus-system. To do this i have a baseclass that contains the functionality for this focussystem. Before i continue i need to know if i should use Custom or UserControl. The problem is that i might need both.

     

    Making a button as CustomControl was easy, just adding 2 templates for focused/unfocused look and change the control-template depending on if the control has focus or not.

    Creating a ListView is a bit more tricky since it has to be dynamicly populated(at least in my app). So, here i've created a CustomControl to represent one option in the listview and the ListView itself is a UserControl that can add options in a StackPanel inside the control.

     

    The problem now is that i have one control deriving from UserControl and one control deriving from CustomControl, i want to derive from MyBaseControl(that derives from eighter Custom- or UserControl), which is impossible now. I could of course create one basecontrol for CustomControls and one identical for UserControls but that feels very B.

    Saturday, September 30, 2006 12:35 PM