官方範例說明
加入 TextBoxInUCControl Property 來存取 UserControl 內的 TextBox
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace UCSerialization
{
public partial class SerializationDemoControl : UserControl
{
private string[] stringsValue = new string[1];
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public string[] Strings
{
get
{
return this.stringsValue;
}
set
{
this.stringsValue = value;
this.textBox1.Text = string.Join(Environment.NewLine, this.stringsValue);
}
}
// 重點
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public TextBox TextBoxInUCControl { get { return textBox1; }}
public SerializationDemoControl()
{
InitializeComponent();
Padding = new Padding(10);
textBox1.Multiline = true;
textBox1.Dock = DockStyle.Fill;
textBox1.ScrollBars = ScrollBars.Vertical;
textBox1.ReadOnly = true;
}
}
}
序列化觀察
透過改變 TextBoxInUCControl DesignerSerializationVisibility 並拖曳到 Form 上來觀察 Form.Designer 內序列化的 Code,下列 Code 只列出觀察重點而已
- DesignerSerializationVisibility.Visible (預設值)
只看見 serializationDemoControl 被序列化而已
private void InitializeComponent()
{
//
// serializationDemoControl1
//
this.serializationDemoControl1.Location = new System.Drawing.Point(188, 185);
this.serializationDemoControl1.Name = "serializationDemoControl1";
this.serializationDemoControl1.Padding = new System.Windows.Forms.Padding(10);
this.serializationDemoControl1.Size = new System.Drawing.Size(130, 50);
this.serializationDemoControl1.Strings = new string[] {null};
this.serializationDemoControl1.TabIndex = 0;
}
- DesignerSerializationVisibility.Content
private void InitializeComponent()
{
//
// serializationDemoControl1
//
this.serializationDemoControl1.Location = new System.Drawing.Point(141, 140);
this.serializationDemoControl1.Name = "serializationDemoControl1";
this.serializationDemoControl1.Padding = new System.Windows.Forms.Padding(10);
this.serializationDemoControl1.Size = new System.Drawing.Size(130, 50);
this.serializationDemoControl1.Strings = new string[] {null};
this.serializationDemoControl1.TabIndex = 0;
//
//
//
this.serializationDemoControl1.TextBoxInUCControl.Dock = System.Windows.Forms.DockStyle.Fill;
this.serializationDemoControl1.TextBoxInUCControl.Location = new System.Drawing.Point(10, 10);
this.serializationDemoControl1.TextBoxInUCControl.Multiline = true;
this.serializationDemoControl1.TextBoxInUCControl.Name = "textBox1";
this.serializationDemoControl1.TextBoxInUCControl.ReadOnly = true;
this.serializationDemoControl1.TextBoxInUCControl.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.serializationDemoControl1.TextBoxInUCControl.Size = new System.Drawing.Size(110, 30);
this.serializationDemoControl1.TextBoxInUCControl.TabIndex = 0;
}
經過上述測試,就能了解該篇文章 - How to: Serialize Collections of Standard Types with the DesignerSerializationVisibilityAttribute 所說的 When your custom controls expose a collection as a property, you can serialize the collection at design time.
設定為 DesignerSerializationVisibility.Content 後,就能直接在 UserControl 上直接對 TextBox 屬性進行變更並序列化至 Form.Designer,假如是 DesignerSerializationVisibility.Visible 的話,雖然可以在屬性視窗內變更 TextBox 屬性,但因為沒有序列化,重開 Form 後設定效果也就不見囉
沒有留言:
張貼留言