在用户控件中持久保留孩子的属性 [英] Persisting properties of a child in a usercontrol

查看:126
本文介绍了在用户控件中持久保留孩子的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉提出这个问题,但是我发现很难在菜鸟级别上找到容易理解的文章/博客. C#,VS2010,桌面应用程序.

我的用户控件由在datagridview和其他子控件上作父母的面板组成.
由于datagridview具有数百个属性,因此我想在设计时在属性编辑器中公开它,并以最简单的方式保留其属性.

Sorry for asking this but I''m finding it very difficult to find an easily understandable article/blog at noob level. C#, VS2010,desktop application.

My user control consists of a panel parenting a datagridview and other child controls.
As a datagridview has hundreds of properties, I wanted to expose it in the property editor at design time and persist it''s properties in the simplest way possible.

public DataGridView View
{
  get { return dgv; }
}


上面的代码没有在designtime属性查看器中公开datagrid视图,但是下面的代码可以显示


The above doesn''t expose the datagrid view in the designtime property viewer, but the following code does

[TypeConverter(typeof(ExpandableObjectConverter))]
public DataGridView View
{
  get { return dgv; }
}


但是,以上内容并未在我的form.designer.cs文件中保留datagridview属性.
我该如何保留其属性?


But, the above doesn''t persist the datagridview properties in my form.designer.cs file.
How do I persist it''s properties please?

推荐答案

我认为我已经做到了.如果有人在苦苦挣扎,我正在报告我的解决方案

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
公共DataGridView视图
{
得到{return dgv; }
}

上面在属性浏览器中公开了DataGridView,并将datagridview保留在我的Form.Designer.cs文件中.它还处理事件:)
I think I''ve done it. I''m reporting my solution in case anybody else is struggling

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public DataGridView View
{
get { return dgv; }
}

The above exposes the DataGridView in the property browser, and persists the datagridview in my Form.Designer.cs file. It also handles events :)


要允许在设计时修改属性,您需要在用户控件中公开它们,因为子级仍然是私有的.如果需要公开子控件的某些属性,则需要一个接一个地进行.这是简单的示例:

To allow properties modification during design time, you need to expose them in your user control as children remains private. If you need to expose some of the properties of the child control, you will need to do it one by one. Here is the simple example:

MyUserControl : UserControl {
   public string TextBoxText { get { return TextBox.Text; } set { TextBox.Text = value; } }
   public Color TextBoxBackColor { get { return TextBox.BackColor; } set { TextBox.BackColor = value; } }
   public Color TextBoxForeColor { get { return TextBox.ForeColor; } set { TextBox.ForeColor = value; } }
   //...
   private TextBox TextBox = new TextBox();
   //...
}



一切都将正确地持续进行,而无需您的任何努力,只是不要使用TypeConverter.

—SA



Everything will persist properly without any effort from you, just don''t use TypeConverter.

—SA


这篇关于在用户控件中持久保留孩子的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆