Reputation: 157
i have a table which contains details of the form contents , here are the details
InputType ( value = Text / Radio / CheckBox , .... )
IsRequired ( True/False)
OrderedAnswers ( contain options available for Radio / checkbox )
....
what i want is ,
make a page at run time which contains respective controls and the page submits the values of each control to the service .
at this point i have created the dataform , but cant define datatemplate dynamically so that i could add stackpanel to it and in the stackpanel i shall add the controls(based on the value). can you provide some code which shall create datatemplate dynamically and items to it ?
thanks
Jamal.
Upvotes: 0
Views: 501
Reputation: 11
I presume you want to add the stackpanel with controls to a particular property (in ria with a Silverlight DataForm)
first capture the event AutoGeneratingField
i.e.
this.myDataForm.AutoGeneratingField +=new EventHandler<DataFormAutoGeneratingFieldEventArgs>(AutoGeneratingFieldHandler);
then in the event handler
void AutoGeneratingFieldHandler(object sender, DataFormAutoGeneratingFieldEventArgs e)
{
if(e.PropertyName=="myPropertyNameWithCustomField")
{
StackPanel pnl = new StackPanel();
pnl.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
ComboBox cbo = new ComboBox();
//setup binding to base
Binding b = new Binding("myPropertyNameWithCustomField") { Mode = BindingMode.TwoWay };
cbo.SetBinding(ComboBox.SelectedValueProperty, b);
//add the combo to the stackpanel
pnl.Children.Add(cbo);
//replace the autogenerated content with the stackpanel
e.Field.Content=pnl;
e.Field.IsRequired=true;
}
}
This should get you started setting up a DataForm with custom controls.
Upvotes: 1