UserControl configuration guidance needed

Oct 3, 2011 at 3:31 AM

Hi!  I have a UserControl that needs to be added multiple times to a View, each time with a different set of configuration settings.  Unfortunately, after watching the "Getting Started" video and reading through the docs I have no idea how to do this.  Any help in this regard would be greatly appreciated!  Thanks....

Oct 3, 2011 at 3:25 PM

More information might help...

What sort of 'userControl' and what kind of configuration settings? What are you trying to accomplish?

Are there bindings needed on the user controls, or are they static, do they require events ('clicks')?

Are the configuration settings, height and width type stuff, or label text and such?

 

Robert

Oct 3, 2011 at 3:33 PM

It’s a UserControl with 6 buttons, a Spinner and a ComboBox. I want to change the content of each these controls at instantiation based upon a configuration parameter (i.e. a MyUserControlKind enumeration)…..

Oct 3, 2011 at 6:12 PM

It depends upon how the parameters are being used. If they are values that will be applied to the UI  elements ( you said content so I assume you are speaking of the button content)  you could accomplish this using dependency properties and pass into each control a predefined value ( by binding or programatically).

Here is a link to a similar quesiton posted on stack overflow.  http://stackoverflow.com/questions/729689/binding-silverlight-usercontrol-custom-properties-to-its-elements that demonstrates how to set this up.  The current question shows textblocks but this could easily be modified for a button. I have used a similar approach for creating forms that are generted at run time (control with a field and a label). The only edit to the code I would suggest is to test within the Dependecy property for null values. If you ever have to bind to an object from a datasource null values will throw a xaml parse exception at run time. Hope this helps.