怎么定义嵌套属性,请各位高手指教(100分)

linlexing 2004-11-23 10:05:35
设计控件时,想定义一个属性,类似于Font之类的,在设计时能展开子属性直接设计。

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace Public.Windows.Forms
{
/// <summary>
/// MonthCalendarCN 的摘要说明。
/// </summary>
public class MonthCalendarCN : System.Windows.Forms.Control
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private Style FTitleStyle ;

public MonthCalendarCN()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent();

// TODO: 在 InitComponent 调用后添加任何初始化
FTitleStyle = new Style();
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}

#region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion

protected override void OnPaint(PaintEventArgs pe)
{
// TODO: 在此添加自定义绘画代码

// 调用基类 OnPaint
base.OnPaint(pe);
}
[DescriptionAttribute("help"),EditorAttribute()]
public Style TilteStyle
{
get
{
return FTitleStyle;
}
set
{
FTitleStyle = value;
}
}



}
}


using System;
using System.Drawing;
namespace Public.Windows.Forms
{
/// <summary>
/// Style 的摘要说明。
/// </summary>
[Serializable]
public class Style : MarshalByRefObject
{
private Font FFont ;
private Color FBackColor ;
private Color FForeColor ;
private Image FBackgroundImage;
public Style()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/*public Font Font
{
get
{
return FFont ;
}
set
{
FFont = value ;
}
}*/
public Color BackColor
{
get
{
return FBackColor ;
}
set
{
FBackColor = value ;
}
}
public Color ForeColor
{
get
{
return FForeColor ;
}
set
{
FForeColor = value ;
}
}

/*public Image BackgroundImage
{
get
{
return FBackgroundImage;
}
set
{
FBackgroundImage = value;
}
}*/

}
}
上面的TilteStyle 就是嵌套属性,但在设计时,该属性显示为灰色,左边也没有加号。是不是要定义EditorAttribute这些特性呀?谁有例子帮帮忙
...全文
116 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
linlexing 2004-11-24
  • 打赏
  • 举报
回复
果然是高手,编译通过了,下面是完整的类代码,多谢了
using System;
using System.ComponentModel;
using System.Drawing;
/// <summary>
/// Style 的摘要说明。
/// </summary>
[Serializable]
[TypeConverter(typeof(StyleConverter))]
public class Style : MarshalByRefObject
{
public readonly static string[] PropertyNames =
new string[] {
"BackColor",
"ForeColor"
};

private Font FFont ;
private Color FBackColor ;
private Color FForeColor ;
private Image FBackgroundImage;
public Style()
{
}

public Style(Color bc, Color fc)
{
this.FBackColor = bc;
this.FForeColor = fc;
}

/*public Font Font
{
get
{
return FFont ;
}
set
{
FFont = value ;
}
}*/

public Color BackColor
{
get
{
return FBackColor ;
}
set
{
FBackColor = value ;
}
}
public Color ForeColor
{
get
{
return FForeColor ;
}
set
{
FForeColor = value ;
}
}

/*public Image BackgroundImage
{
get
{
return FBackgroundImage;
}
set
{
FBackgroundImage = value;
}
}*/

public override string ToString()
{
// 没有实现
return base.ToString ();
}

/// <summary>
/// 从字符串解析出实例
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Style Parse(string source)
{
// 没有实现
return null;
}
}

public class StyleConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
return true;
return base.CanConvertTo(context, destinationType);
}

public override object ConvertFrom (ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
// 从字符串中解析出 Style 实例,请自已实现。
return Style.Parse(value as string);
}
return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
Style gs = (Style)value;
// Style 的 ToString() 请自行实现。
return gs.ToString();
}
if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
{
Style gs = (Style)value;
return new System.ComponentModel.Design.Serialization.InstanceDescriptor(
typeof(Style).GetConstructor(
new Type[2]{typeof(Color), typeof(Color)}),
new Color[2]{gs.BackColor, gs.ForeColor});
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}


public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
return new Style((Color)propertyValues[Style.PropertyNames[0]],
(Color)propertyValues[Style.PropertyNames[1]]);
}

public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}

public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context,
object value, Attribute[] attributes)
{
PropertyDescriptorCollection properties;
properties = TypeDescriptor.GetProperties(value, attributes);

return properties.Sort(Style.PropertyNames);
}
}
AhBian 2004-11-23
  • 打赏
  • 举报
回复
原提供的代码中还有两个被注释掉的属性,我没有实现。
楼主可以照样继续实现。
AhBian 2004-11-23
  • 打赏
  • 举报
回复
以下代码未经测试,有问题在所难免。
时间仓促,也未写出详细的注释,请见谅。

大部分是 VS.NET 标准接口的实现,可以通过参阅帮助系统中的相关条目获取详细的介绍。

/// <summary>
/// Style 的摘要说明。
/// </summary>
[Serializable]
[TypeConverter(typeof(StyleConverter))]
public class Style : MarshalByRefObject
{
public readonly static string[] PropertyNames =
new string[] {
"BackColor",
"ForeColor"
};

private Font FFont ;
private Color FBackColor ;
private Color FForeColor ;
private Image FBackgroundImage;
public Style()
{
}

public Style(Color bc, Color fc)
{
this.FBackColor = bc;
this.FForeColor = fc;
}

/*public Font Font
{
get
{
return FFont ;
}
set
{
FFont = value ;
}
}*/

public Color BackColor
{
get
{
return FBackColor ;
}
set
{
FBackColor = value ;
}
}
public Color ForeColor
{
get
{
return FForeColor ;
}
set
{
FForeColor = value ;
}
}

/*public Image BackgroundImage
{
get
{
return FBackgroundImage;
}
set
{
FBackgroundImage = value;
}
}*/

public override string ToString()
{
// 没有实现
return base.ToString ();
}

/// <summary>
/// 从字符串解析出实例
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static Style Parse(string source)
{
// 没有实现
}
}

public class StyleConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}

public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
return true;
return base.CanConvertTo(context, destinationType);
}

public override object ConvertFrom (ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
// 从字符串中解析出 Style 实例,请自已实现。
return Style.Parse(value as string);
}
return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
Style gs = (Style)value;
// Style 的 ToString() 请自行实现。
return gs.ToString();
}
if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
{
Style gs = (Style)value;
return new System.ComponentModel.Design.Serialization.InstanceDescriptor(
typeof(Style).GetConstructor(
new Type[2]{typeof(Color), typeof(Color)}),
new Color[2]{gs.BackColor, gs.ForeColor});
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}


public override object CreateInstance(ITypeDescriptorContext context, System.Collections.IDictionary propertyValues)
{
return new Style((Color)propertyValues[Style.PropertyNames[0]],
(Color)propertyValues[Style.PropertyNames[1]]);
}

public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}

public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context,
object value, Attribute[] attributes)
{
PropertyDescriptorCollection properties;
properties = TypeDescriptor.GetProperties(value, attributes);

return properties.Sort(Style.PropertyNames);
}
}
linlexing 2004-11-23
  • 打赏
  • 举报
回复

没人遇到这个问题吗

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧