开发者

Silverlight 4 + Prism create controls at runtime

I'm creating a Silverlight 4 application to manage active directory objects (CRUD Users, groups etc). Now we all know that active directory objects have lots of properties. I want to save some groups of property names in an SQL DB (ie, samaccountname, mail, etc.) for certain AD object types (users, groups, etc). When a user in the silverlight app clicks on "create user" I want to

  1. get all the property names I defined in the sql db for the user
  2. create a pre defined silverlight control for each property and add it to the current "create view"
  3. be able to read all the values the user enters in the controls and pass them on

The user controls d开发者_开发百科iffer depending on the type of the Active Directory property, i.e. for a property with a datetime value, we will create a user control with a calendar. This way we can keep the "Create" Views dynamic.

The Silverlight / Prism foundation is already there and I'm adding my Views via the RegionManager. It all works just fine but I'm not sure how to add controls in such a dynamic way that it still works with PRISM and the MVVM pattern. Thank you for any input.


I'm going to assume that your comfortable with the getting the properties from the db and saving the values back and focus on the middle area of creating the view with MVVM in mind.

The PRISM part I wouldn't worry about. You say you've got the views registered with the region manager and that's about all you need to do.

My initial thought was how would you "create" controls at runtime with MVVM considered. Creating controls is not the hard part but would require a handle on the element you will be injecting these into, a root grid for example. This would on the surface feel very un-MVVM. Personally I think it's valid to have "view" code done the old fashion way, it's the "data" code used in binding that should be MVVM'd. The issue is telling the code that will inject the controls on to the view what to actually inject. I can't think of a neat way to do this.

As a solution could you not create a view containing all the controls that apply to each of the properties and rather than "create" them when needed "hide" them when not needed? You could bind the "Visibility" property (probably through a value converter) to a property in the ViewModel that can be used to determine if that particular control should be shown or not. If these where placed in a stack panel then the view would dynamically shrink.

If you think a code example would help let me know.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜