locked
How to add components to a resource dictionary? RRS feed

  • Question

  • I am trying to create a library of standardized components that can be used from one project to another (from page layouts to individual components). The only way I have so far figured out this might be done is through the resource dictionaries, but I can't figure out how to add a component to a dictionary (drag-and-drop from the resources panel doesn't work and when I right-click, the "copy" option isn't available on my components). There is a component option to "link to a resource dictionary," but this doesn't seem to actually add it to the dictionary.

    Is there another solution for my problem (creating a stock set of page and components for various projects)?
    Thanks for any help,
    Linda
    Friday, December 4, 2009 5:09 PM

Answers

  • Howdy,

    Sure, you can create a new Project and call it something like SharedComponents.

    In this new project, create UserControls for every page and add all the elements and logic required. You can even reference the styles inside RD's in that project as well.

    Reference that project from any other project that you need to use them and then just instantiate your UserControl(s) as required.

    UserControls are more about being a presentation surface for both WPF and Silverlight rather than an actual control as WinForms creates.

    So Project 1 (Main Project)
    - Contains MainWindow

    Project 2 (SharedComponents)
    - Contains Page1
    Page2
    Page3

    In MainWindow, one you reference Project2 (In the solution explorer Right click on Project 1 and select Add Reference) you can then use the Pages you have created in your other project.

    Cheers

    Expression MVP

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Friday, December 4, 2009 8:18 PM
    Moderator

All replies

  • Howdy Linda,

    The concept of the ResourceDictionary (RD) is around the seperation of Styles that are applied to elemnents (or components), so instead of storing the controls, you store the styles (and templates) inside the RD.

    Short sample to try is to start a new project and add a border or rectangle and style it as you wish (like a button perhaps)

    Go the to Tools-> Make into control. Here you can make your Border into a style template of your choice. Find Button in the list (It should be defaulted to that) and at the bottom of that dialog you should see an option for Resource Dictionary as the storage location. You can select new to create one of an existing one and then click OK.

    You will now be editing the actual template of the button. You can go back to your Window by using the bread crumb at the top of your screen. From there you can now add a Button control to your Window, and then open the RD where you stored the Style. Drag the Style onto the button and it should take the style you just created.

    Hope that gives you a starting point. Have a look through the help files that come with blend and you will see some really good tutorials that will give you a better grasp of the basic concepts that you need.

    Cheers

    Expression MVP

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Friday, December 4, 2009 7:36 PM
    Moderator
  • HI Brennan-
    Thanks for your reply - I appreciate it; I think I understand what you are saying about the dictionaries storing styles. What I still need to figure out, however, is a way to save component pages as re-usable pieces that can be applied to different projects. Is that possible? Without that ability, we have to recreate the wheel for all the pieces and parts that we re-use in each of our projects.
    Thanks,
    Linda
    Friday, December 4, 2009 8:11 PM
  • Howdy,

    Sure, you can create a new Project and call it something like SharedComponents.

    In this new project, create UserControls for every page and add all the elements and logic required. You can even reference the styles inside RD's in that project as well.

    Reference that project from any other project that you need to use them and then just instantiate your UserControl(s) as required.

    UserControls are more about being a presentation surface for both WPF and Silverlight rather than an actual control as WinForms creates.

    So Project 1 (Main Project)
    - Contains MainWindow

    Project 2 (SharedComponents)
    - Contains Page1
    Page2
    Page3

    In MainWindow, one you reference Project2 (In the solution explorer Right click on Project 1 and select Add Reference) you can then use the Pages you have created in your other project.

    Cheers

    Expression MVP

    my blog : http://x-coders.com/blogs/sneaky/default.aspx

    Friday, December 4, 2009 8:18 PM
    Moderator
  • Perfect! That is exactly what I needed to know. Thanks so much, again, Brennon.
     - Linda
    Friday, December 4, 2009 8:40 PM
  • Please remember to mark your question as answered!  Thanks!  :)
    Saturday, December 5, 2009 4:01 AM