Sometimes you need to create some UI controls using C# code, in this case, you can:
Note: For serializing the UI element into a XAML file, you need to use the System.Windows.Markup.XamlWriter.Save() instead of the System.Xaml.XamlServices. For the WPF/Silverlight is not supported by the XamlServices.
UserControl form = new UserControl(); Grid layoutRoot = new Grid(); form.Content = layoutRoot; StackPanel rootStack = new StackPanel() { HorizontalAlignment = System.Windows.HorizontalAlignment.Left, VerticalAlignment = System.Windows.VerticalAlignment.Top, Width = 700, Height = 1000 }; layoutRoot.Children.Add(rootStack); foreach (Field f in Def.AllForms[0].AllFields) { StackPanel curField = new StackPanel { Orientation = Orientation.Horizontal, HorizontalAlignment = System.Windows.HorizontalAlignment.Left, Width = 500, Height = 20, Children = { new TextBlock() { Text = f.Name, Width = 110, Margin = new System.Windows.Thickness(5, 0, 5, 0) }, new TextBox() { Width = 220 } }, Margin = new System.Windows.Thickness(20,5,0,5) }; rootStack.Children.Add(curField); } System.Windows.Markup.XamlWriter.Save(form, new StreamWriter(@"D:/FormDef1.xaml")); |