I have Add a dynamically created Silverlight User control in another Silverlight User Control. I create this control from a XML file data.
Here are the step and necessary Code for reference:
Step-1: Add the usercontrol as per requirement.
Step-2: In your MainPage.xml
Add : xmlns:local="clr-namespace:ProjectSample"
< Grid x:Name="LayoutRoot">
< Grid.RowDefinitions>
< RowDefinition Height="550*"/>
< RowDefinition Height="50"/>
< Grid.ColumnDefinitions>
< ColumnDefinition Width="Auto"/>
< Grid Grid.Row="0" Grid.Column="0">
< controlsToolkit:WrapPanel Orientation="Vertical" x:Name="AddressPanel" HorizontalAlignment="Left"/>
< Button x:Name="Save" Grid.Row="1" Grid.Column="0" Click="Save_Click" Content="Save" Height="22" Width="60" HorizontalAlignment="Center" VerticalAlignment="Top" TabIndex="8" Cursor="Hand"/>
Step-3: In your MainPage.xml.cs
ucAddress ucObj;
for (int i = 0; i < 5; i++)
{
ucObj = new ucAddress();
ucObj.Name = "ucName" + i;
AddressPanel.Children.Add(ucObj);
}
Step-3: Retriving the data from usercontrol and inserting it.
private void Save_Click(object sender, RoutedEventArgs e)
{
User usr;
for (int i = 0; i < 5; i++)
{
ucObj = (ucAddress)LayoutRoot.FindName("ucName" + i);
usr = new User();
usr.Email = "test@t.com";
usr.Firstname = ucObj.FirstNameData.Text;
usr.Lastname = ucObj.LastNameData.Text;
usr.Username = ucObj.CityData.SelectionBoxItem.ToString();
usr.Password = "pa55w0rd!";
usr.Sex = "M";
_RegDContext.Users.Add(usr);
}
_submitOp = _RegDContext.SubmitChanges();
_submitOp.Completed +=new EventHandler(_submitOp_Completed);
}
Hope this will be useful & your comments are mostly welcome.
No comments:
Post a Comment