Reputation: 270
I am working on program that will render questionnaire forms from a database using Silverlight with C#.
I currently have it rendering UserControls based on a Question records "Control" field. "Textbox", "Label" etc etc.
Im just wondering if it's possible to load attributes for these controls based on an attributes table. For instance I would have an Attributes table containing name value pairs such as "Padding, 10", "Margin, 5" etc etc. linked to a particular Question.
Should create a method that will apply these attributes based on a large case statement. I've also though of using reflection but this may give a big performance hit. Are there any better solutions?
Thanks.
Upvotes: 0
Views: 280
Reputation: 270
I've found a solution. Beforehand I was rendering textboxes just using the Controls classes. Now I am building up the XAML and using XamlReader.Load()
to turn the xaml into a control class.
public UIElement GetControl()
{
StringBuilder sb = new StringBuilder();
sb.Append("<TextBox xmlns='http://schemas.microsoft.com/client/2007'> ");
sb.Append(Model.QuestionAttributes.DrawAttributes());
sb.Append("/>");
TextBox Textbox = XamlReader.Load(sb.ToString()) as TextBox;
return Textbox as TextBox;
}
Upvotes: 1