Jag*_*san 7 .net c# user-controls windows-forms-designer winforms
我创建了一个自定义控件和组件,如下代码所示,
public class CustomComponent : Component
{
private string style;
public CustomControl Control { get; set; }
public string Style
{
get
{
return style;
}
set
{
style = value;
Control.Style = value;
}
}
}
public class CustomControl : Control
{
string style;
public string Style
{
get
{
return style;
}
set
{
style = value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
之后,我将控件添加到表单中,并将组件添加到表单中。然后尝试分配 Component.Control 值。分配值后,如果我尝试更改组件的样式属性,控件中的样式属性在设计器级别不会更改,如下图所示,
如果我单击了控件的 Style 属性,它将被更新,如下图所示,
您需要更正代码中的一些内容。Style您的属性应CustomComponent更改为:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[RefreshProperties(System.ComponentModel.RefreshProperties.All)]
public string Style
{
get
{
if (Control != null)
return Control.Style;
else
return null;
}
set
{
if (Control != null)
Control.Style = value;
}
}
Run Code Online (Sandbox Code Playgroud)
您应该检查是否Control不是,获取或设置Style控件的值。当属性值属于另一个控件时,您不需要定义成员变量来存储属性值。
另外,由于您不需要序列化组件的属性(因为它已为您的控件序列化),所以用DesignerSerializationVisibility具有值的属性来装饰它Hidden。
此外,当您在编辑组件的属性时想要刷新PropertyGrid以显示其他属性(如Control.Style属性)的更改时,请使用具有值的属性来装饰它。StyleRefreshPropertiesRefreshProperties.All
| 归档时间: |
|
| 查看次数: |
2382 次 |
| 最近记录: |