简体   繁体   中英

How to Set a Collection of Custom Controls from the property GridView in c#

I have an ObservableCollection of MySearchFields, on a custom Form, lets say CustomForm I declare it as below:

private ObservableCollection<MySearchFields> _mySearchFields = new ObservableCollection<MySearchFields>();
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Category("MyFrameWork")]
        public ObservableCollection<MySearchFields> MySearchFields
        {
            get => _mySearchFields;
        }

and then on my Main form where it inherits from CustomForm, when I'm on the designer view and go to propertygrid window I select my ControlType To be ComboBox and then I select DataSource, ValueMember And Display Member. the issue is it does not set DataSource,ValueMember or DisplayMember. why does this happen? am I missing something?

 public class MySearchFields
{

    public enum MyControlTypes
    {
        MyTextBox,            
        MyDateTimePicker,
        MyLbComboBox
    }

    private MyControlTypes _myControlType = MyControlTypes.MyTextBox;

    public MyControlTypes MyControlType
    {
        get => _myControlType;
        set
        {
            _myControlType = value;
            switch (_myControlType)
            {
                case MyControlTypes.MyTextBox:
                {
                    MyControl = new TextBox();
                    break;
                }

                case MyControlTypes.MyLbComboBox:
                {
                    MyControl = new MyLbComboBox();                        
                    break;
                }
                case MyControlTypes.MyDateTimePicker:
                {
                    MyControl = new DateTimePicker();
                    break;
                }

            }
        }
    }

    public Control MyControl { get; set; }
    public MySearchFields()
    {
        MyControl = new Control();                  
    }
}

Your error is that on your case switcher you insert new MyLBcombobox instead of just ComboBox.

try initializing public Control MyControl { get; set; } with the proper setters and getters, as well as a private variable the backs it up,

Furthermore just use the fullprop snippet of Visual studio

just write "fullprop" and the press 2 times TAB, swapping around => and {} is not that nice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM