예제 #1
0
        void AddField(CrudDecoratorAttribute decorator)
        {
            var field = new CrudField(decorator, Model);

            tbFields.Controls.Add(field, 0, _currentRowIndex++);

            field.OnError += Field_OnError;
        }
예제 #2
0
        internal IEnumerable <CrudDecoratorAttribute> GetDecorators()
        {
            foreach (var p in GetModelFields())
            {
                var a = p.GetCustomAttribute <CrudDecoratorAttribute>();

                if (a == null)
                {
                    a         = new CrudDecoratorAttribute();
                    a.Label   = p.Name;
                    a.Control = typeof(TextBox);
                }

                a.Property = p;
                yield return(a);
            }
        }
예제 #3
0
        public CrudField(CrudDecoratorAttribute decorator, ModelBase model)
        {
            Model = model;

            InitializeComponent();

            EditControl = EditorFactory.CreateEditor(decorator, model);

            lbFieldLabel.Text = decorator.Label;
            pnlEdit.Controls.Add(EditControl);

            EditControl.Font = lbFieldLabel.Font;

            EditControl.Width  = decorator.Width;
            EditControl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Top;
            EditControl.Top    = (pnlEdit.Height - EditControl.Height) >> 1;

            if (!decorator.AllowEdit)
            {
                EditControl.Enabled = false;
            }

            Tag = decorator;
        }
예제 #4
0
        public static Control CreateEditor(CrudDecoratorAttribute decorator, ModelBase model)
        {
            Control editor = null;

            var type = decorator.Property.PropertyType;

            if (type.Equals(typeof(DateTime)))
            {
                var p = new DateTimePicker();
                p.CustomFormat = "dd/MM/yyyy";
                p.Format       = DateTimePickerFormat.Short;
                editor         = p;

                p.DataBindings.Add("Value", model, decorator.Property.Name);
            }

            string pn = decorator.Property.Name;

            if (pn.ToLower().EndsWith("id"))
            {
                var tp = Type.GetType($"{model.GetType().Namespace}.{pn.Substring(0, pn.Length-2)}", false, true);

                if (tp != null && !tp.Equals(model.GetType()))
                {
                    var cb = new ComboBox();
                    cb.FormattingEnabled = true;

                    var fd = (ModelBase)Activator.CreateInstance(tp);

                    cb.DataSource = fd.GetController().GetList();
                    editor        = cb;

                    PropertyInfo id = null;

                    foreach (var p in tp.GetProperties())
                    {
                        if (p.Name.Equals($"{tp.Name}id", StringComparison.CurrentCultureIgnoreCase))
                        {
                            id = p;
                            break;
                        }
                    }

                    if (id != null)
                    {
                        cb.ValueMember = id.Name;
                    }

                    cb.DataBindings.Add("SelectedValue", model, pn);
                }
            }

            if (type.IsEnum)
            {
                var cb = new ComboBox();

                var            names  = Enum.GetNames(type);
                var            values = Enum.GetValues(type);
                List <EnumMap> map    = new List <EnumMap>();

                int index = 0;

                foreach (var e in type.GetMembers())
                {
                    if (!names.Contains(e.Name))
                    {
                        continue;
                    }

                    var    a    = e.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    string name = null;

                    if (a != null && a.Length > 0)
                    {
                        name = ((DescriptionAttribute)a[0]).Description;
                    }
                    else
                    {
                        name = e.Name;
                    }

                    map.Add(new EnumMap()
                    {
                        Name = name, Value = values.GetValue(index)
                    });
                    index++;
                }

                cb.DataSource  = map;
                cb.ValueMember = "Value";

                cb.DataBindings.Add("SelectedValue", model, decorator.Property.Name);

                editor = cb;
            }

            if (editor == null)
            {
                editor = new TextBox();
                editor.DataBindings.Add("Text", model, decorator.Property.Name);
            }

            return(editor);
        }