Exemple #1
0
        public void Reflect()
        {
            spnlControls.Children.Clear();


            if (lbxObjects.SelectedItem != null)
            {
                Type temp = lbxObjects.SelectedItem.GetType();
                foreach (PropertyInfo pi in lbxObjects.SelectedItem.GetType().GetProperties())
                {
                    if (pi.CanWrite)
                    {
                        PropertyEditor ed = new PropertyEditor();

                        // get type and cast selected object to this type?

                        ed.lblName.Content = pi.Name;
                        Type type = lbxObjects.SelectedItem.GetType().GetProperty(pi.Name).PropertyType;
                        ed.lblName.ToolTip = pi.PropertyType.IsPublic.ToString();
                        UIElement ue  = null;
                        UIElement ue2 = null;
                        switch (pi.PropertyType.Name.ToLower())
                        {
                        case "string":
                        {
                            TextboxExt tb  = new TextboxExt();
                            object     obj = pi.GetValue(lbxObjects.SelectedItem, null);
                            if (obj != null)
                            {
                                tb.Text           = obj.ToString();
                                tb.ObjectToModyfy = obj;
                            }
                            else
                            {
                                tb.ObjectToModyfy = "";
                            }
                            tb.PropertyName = pi.Name;
                            tb.ToolTip      = pi.PropertyType.Name;
                            tb.Height       = 20;
                            ue = tb;
                            break;
                        }

                        case "list`1":
                        {
                            TextboxExt tb    = new TextboxExt();
                            Type       tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem, null));

                            tb.Text    = tTemp.Name;
                            tb.ToolTip = "(" + tTemp.Name + ") list<>";

                            tb.PropertyName = pi.Name;
                            tb.Height       = 20;
                            tb.IsEnabled    = false;
                            ButtonExt btn = new ButtonExt();
                            btn.ObjectToModyfy      = pi.GetValue(lbxObjects.SelectedItem, null);
                            btn.PropertyName        = pi.Name;
                            btn.Content             = "[..]";
                            btn.HorizontalAlignment = HorizontalAlignment.Left;
                            btn.Click += new RoutedEventHandler(btn_Click);
                            btn.Width  = 20;
                            ue2        = btn;
                            ue         = tb;

                            break;
                        }

                        case "double":
                        {
                            TextboxExt tb = new TextboxExt();
                            tb.Text           = pi.GetValue(lbxObjects.SelectedItem, null).ToString();
                            tb.PropertyName   = pi.Name;
                            tb.ObjectToModyfy = pi.GetValue(lbxObjects.SelectedItem, null);
                            if (tb.ObjectToModyfy == null)
                            {
                                tb.ObjectToModyfy = "";
                            }
                            tb.Height  = 20;
                            tb.ToolTip = pi.PropertyType.Name;
                            ue         = tb;
                            break;
                        }

                        case "int32":
                        {
                            TextboxExt tb = new TextboxExt();
                            tb.Text           = pi.GetValue(lbxObjects.SelectedItem, null).ToString();
                            tb.PropertyName   = pi.Name;
                            tb.ObjectToModyfy = pi.GetValue(lbxObjects.SelectedItem, null);
                            tb.Height         = 20;
                            tb.ToolTip        = pi.PropertyType.Name;
                            ue = tb;
                            break;
                        }

                        case "boolean":
                        {
                            CheckBoxExt chk = new CheckBoxExt();
                            chk.PropertyName = pi.Name;
                            object obj = pi.GetValue(lbxObjects.SelectedItem, null);
                            chk.ObjectToModyfy = obj;
                            chk.IsChecked      = (bool)obj;
                            chk.ToolTip        = pi.PropertyType.Name;
                            ue = chk;
                            break;
                        }

                        default:
                        {
                            if (pi.PropertyType.IsEnum)
                            {
                                ComboBoxExt bx = new ComboBoxExt();
                                bx.PropertyName      = pi.Name;
                                bx.DisplayMemberPath = "Name";
                                bx.ToolTip           = pi.PropertyType.Name;
                                foreach (FieldInfo feif in pi.PropertyType.GetFields())
                                {
                                    if (feif.Name != "value__")
                                    {
                                        bx.Items.Add(feif);
                                    }
                                }
                                bx.ObjectToModyfy = pi.GetValue(lbxObjects.SelectedItem, null);
                                bx.SelectedItem   = pi.PropertyType.GetField(pi.GetValue(lbxObjects.SelectedItem, null).ToString());
                                ue = bx;
                            }
                            else if (pi.PropertyType.IsGenericType &&
                                     pi.PropertyType.GetGenericTypeDefinition() ==
                                     typeof(Nullable <>))
                            {
                                TextBox tb    = new TextBox();
                                Type    tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem, null));

                                Type propType = pi.PropertyType;
                                if (propType.IsGenericType &&
                                    propType.GetGenericTypeDefinition() ==
                                    typeof(Nullable <>))
                                {
                                    Type[] typeCol = propType.GetGenericArguments();
                                    Type   nullableType;
                                    if (typeCol.Length > 0)
                                    {
                                        nullableType = typeCol[0];
                                        if (nullableType.BaseType == typeof(Enum))
                                        {
                                            ComboBox bx = new ComboBox();
                                            foreach (FieldInfo feif in nullableType.GetFields())
                                            {
                                                if (feif.Name != "value__")
                                                {
                                                    bx.Items.Add(feif);
                                                }
                                            }
                                            ue = bx;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                ButtonExt btn = new ButtonExt();
                                btn.PropertyName   = pi.Name;
                                btn.Content        = pi.PropertyType.Name;
                                btn.ObjectToModyfy = pi.GetValue(lbxObjects.SelectedItem, null);
                                btn.EditingType    = pi.PropertyType;
                                btn.Click         += new RoutedEventHandler(btn_ClickOeditor);
                                ue = btn;
                            }
                            break;
                        }
                        }
                        if (ue != null)
                        {
                            ue.SetValue(Grid.ColumnProperty, 1);
                            ue.SetValue(Grid.VerticalAlignmentProperty, VerticalAlignment.Center);
                            ed.ContentGrid.Children.Add(ue);
                            if (ue2 != null)
                            {
                                ue2.SetValue(Grid.ColumnProperty, 2);
                                ue2.SetValue(Grid.VerticalAlignmentProperty, VerticalAlignment.Center);
                                ed.ContentGrid.Children.Add(ue2);
                            }
                        }

                        spnlControls.Children.Add(ed);
                    }
                }
            }
        }