如何在UserControl中序列化DateTimePicker的设计器属性值?

问题描述

在Visual Studio设计器C#中,我有一个名为 MyControl 的UserControl。我添加一个名为 dateTimePicker1 的DateTimePicker控件。在MyControl代码中。我想对公共 MyControl.MyPicker 进行编码,以将 dateTimePicker1 公开给设计者和我的应用程序代码。我需要帮助使设计时间序列化正常工作。例如,在设计器中设置MyPicker.Checked值不会序列化。

这就是我现在在MyControl.cs中拥有的

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public DateTimePicker MyPicker
    { 
      get { return this.dateTimePicker1; }
      set { this.dateTimePicker1 = value; }
    }

当我将 MyControl 放到应用程序的Form1中时,将其重命名为myDateTime,然后在设计器中设置以下属性

// I set these property values in the designer interactively
MyPicker.AllowDrop = true;
MyPicker.CustomFormat = "MM/dd/yyyy";
MyPicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
//... I also set Font and Value properties in the designer
MyPicker.Checked = true

结果是Form1.Designer.cs中的以下代码

      // 
      // myDateTime
      // 
      this.myDateTime.MyPicker.AllowDrop = true;
      this.myDateTime.MyPicker.CustomFormat = "MM/dd/yyyy";
      this.myDateTime.MyPicker.Dock = System.Windows.Forms.DockStyle.Fill;
      this.myDateTime.MyPicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
      this.myDateTime.MyPicker.Location = new System.Drawing.Point(0,0);
      this.myDateTime.MyPicker.MaxDate = new System.DateTime(2222,12,31,0);
      this.myDateTime.MyPicker.Name = "dateTimePicker1";
      this.myDateTime.MyPicker.ShowCheckBox = true;
      this.myDateTime.MyPicker.TabIndex = 1;
      this.myDateTime.MyPicker.Value = new System.DateTime(1970,1,0);
      this.myDateTime.Font = new System.Drawing.Font("DejaVu Sans Mono",9.25F);
      this.myDateTime.Location = new System.Drawing.Point(50,154);
      this.myDateTime.Name = "myDateTime";
      this.myDateTime.Size = new System.Drawing.Size(200,22);
      this.myDateTime.TabIndex = 7;

myDateTime.MyPicker.Checked 属性丢失! DateTimePicker未可靠地序列化。我认为其他一些属性也未序列化。现在,我主要关心的是简单的Checked属性

我知道我可以为MyControl.DateTimePickerIsChecked创建一个单独的公共属性,然后获取并设置嵌入的DateTimePicker.Checked属性。但是,我认为设计器不是不一致的,所以我必须缺少一些东西!

有人知道如何使设计器的序列化与UserControl中的DateTimePicker可靠地一起工作吗?还是我需要其他属性属性

平台:Windows 10 LTSC 1809,Visual Studio 2019 Pro版本16.7.2,.NET 4.6.2,Windows窗体

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)