Esempio n. 1
0
        /// <summary>
        /// Generating form using grid layout base on Persion object
        /// </summary>
        /// <param name="persion"></param>
        private void CreateFormUsingObject(Person persion)
        {
            Grid rootGrid = new Grid();
            rootGrid.Margin = new Thickness(10.0);
            rootGrid.ShowGridLines = true;
            //definition column
            rootGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(100.0) });
            rootGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) });
          
            PropertyInfo[] propertyInfos;
            propertyInfos = typeof (Person).GetProperties();
           
            //definition row index
            int j = 0;

            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                if (propertyInfo.PropertyType.Name == "String")
                {
                    //definition one row
                    rootGrid.RowDefinitions.Add(CreateRowDefinition());
                    TextBlock label = CreateTextBlock(propertyInfo.Name, j, 0);
                    //add to grid
                    rootGrid.Children.Add(label);

                    TextBox textBox = CreateTextBox(j, 1);
                    //add to grid
                    rootGrid.Children.Add(textBox);
                    j++;
                }

                if (propertyInfo.PropertyType.Name == "Boolean")
                {
                    //definition one row
                    rootGrid.RowDefinitions.Add(CreateRowDefinition());
                    TextBlock label = CreateTextBlock(propertyInfo.Name, j, 0);
                    //add to grid
                    rootGrid.Children.Add(label);

                    CheckBox checkBox = CreateCheckBox("Text",j, 1);
                    //add to grid
                    rootGrid.Children.Add(checkBox);
                    j++;
                }

                if (propertyInfo.PropertyType.Name == "Int32")
                {
                    //definition one row
                    rootGrid.RowDefinitions.Add(CreateRowDefinition());
                    TextBlock label = CreateTextBlock(propertyInfo.Name, j, 0);
                    //add to grid
                    rootGrid.Children.Add(label);

                    TextBox textBox = CreateTextBox(j, 1);
                    //add to grid
                    rootGrid.Children.Add(textBox);
                    j++;
                }
            }
            //Add grid to layout root
            LayoutRoot.Children.Add(rootGrid);
        }
Esempio n. 2
0
 private void Grid_Loaded(object sender, RoutedEventArgs e)
 {
     Person persion = new Person();
     CreateFormUsingObject(persion);
 }