private class NodeProps
{ [Category("Layout")]
[Description("节点显示的文本值.")]
public string Text
{
get { return _text; }
set { _text = value; }
}
[Description("节点角色值的选择")]
[Category("Behavior")]
public MyAction Action
{
get { return Action1; }
set { Action1 = value; } }
}这是我自定义的属性,现在的问题是在用户传入参数后我要把这些属性的参数全部保存为xml文件,改怎么做啊?下来还有一问题就是我继承的于.NET原有属性比如text,在用户输入参数后可以保存下来,而我地定义的 action这个属性就不能保存,希望各位高手赐教啊!!!!!
{ [Category("Layout")]
[Description("节点显示的文本值.")]
public string Text
{
get { return _text; }
set { _text = value; }
}
[Description("节点角色值的选择")]
[Category("Behavior")]
public MyAction Action
{
get { return Action1; }
set { Action1 = value; } }
}这是我自定义的属性,现在的问题是在用户传入参数后我要把这些属性的参数全部保存为xml文件,改怎么做啊?下来还有一问题就是我继承的于.NET原有属性比如text,在用户输入参数后可以保存下来,而我地定义的 action这个属性就不能保存,希望各位高手赐教啊!!!!!
[TypeConverter(typeof(ExpandableObjectConverter))] //<--- to be editable
public class MyAction
{
public string ActionName { get; set; }
public int Strength { get; set; }
public override string ToString() {return this.ActionName + " " + this.Strength;}
} public class NodeProps
{
public string Text { get; set; }
public MyAction Action { get; set; } public NodeProps()
{
this.Text = "hello";
this.Action = new MyAction();
}
} private void button1_Click(object sender, EventArgs e)
{
NodeProps p = this.propertyGrid1.SelectedObject as NodeProps;
XmlSerializer serializer = new XmlSerializer(typeof(NodeProps));
using (StreamWriter sw = new StreamWriter("c:\\temp\\n.xml"))
{
serializer.Serialize(sw, p); //<--- save as xml
}
}
//控件的宽度
protected int _ControlWidth;
public int ControlWidth
{
set { this._ControlWidth = value; }
get { return this._ControlWidth; }
}
//空间高度
protected int _ControlHeight;
public int ControlHeight
{
set { this._ControlHeight = value; }
get { return this._ControlHeight; }
}
//表格宽度
protected int _ItemWidth;
public int ItemWidth
{
set { this._ItemWidth = value; }
get { return this._ItemWidth; }
}
//表格高度
protected int _ItemHeight;
public int ItemHeight
{
set { this.ItemWidth = value; }
get { return this.ItemHeight; }
}
//table高度
protected int _TableHeight;
public int TableHeight
{
set { this._TableHeight = value; }
get { return this._TableHeight; }
}
//td1宽度
protected int _TdWidth;
public int TdWidth
{
set { this._TdWidth = value; }
get { return this._TdWidth; }
}
//td2宽度
protected int _TdWidth2;
public int TdWidth2
{
set { this._TdWidth2 = value; }
get { return this._TdWidth2; }
}
//td3宽度
protected int _TdWidth3;
public int TdWidth3
{
set { this._TdWidth3 = value; }
get { return this._TdWidth3; }
}
//td的高度
protected int _TdHeight;
public int TdHeight
{
set { this._TdHeight = value; }
get { return this._TdHeight; }
}
//td2的高度
protected int _TdHeight2;
public int TdHeight2
{
set { this._TdHeight2 = value; }
get { return this._TdHeight2; }
}
//td3的高度
protected int _TdHeight3;
public int TdHeight3
{
set { this._TdHeight3 = value; }
get { return this._TdHeight3; }
}
//跳转的URL
protected string _NewsPage = "NewsDetail";
public string NewsPage
{
set { this._NewsPage = value; }
get { return this._NewsPage; }
}
//背景
protected string _BodyBackgroundurl;
public string BodyBackgroundurl
{
set { this._BodyBackgroundurl = value; }
get { return this._BodyBackgroundurl; }
}
//行背景
protected string _Tdbackgroud;
public string Tdbackground
{
set { this._Tdbackgroud = value; }
get { return this._Tdbackgroud; }
}
//样式
protected string _LinkStyle;
public string LinkStyle
{
set { this._LinkStyle = value; }
get { return this._LinkStyle; }
}
//td样式
protected string _TdStyle;
public string TdStyle
{
set { this._TdStyle = value; }
get { return this._TdStyle; }
}
//图标
protected string _IconUrl;
public string IconUrl
{
set { this._IconUrl = value; }
get { return this._IconUrl; }
}
//图片宽度
protected int _ImgWidth;
public int ImgWidth
{
set { this._ImgWidth = value; }
get { return this._ImgWidth; }
}
//图片高度
protected int _ImgHeight;
public int ImgHeight
{
set { this._ImgHeight = value; }
get { return this._ImgHeight; }
}
//父类ID1
protected int _ParentID;
public int ParentID
{
set { this._ParentID = value; }
get { return this._ParentID; }
}
//每行显示多少条目
protected int _DisplayRowsCount = 1;
public int DisplayRowsCount
{
set { this._DisplayRowsCount = value; }
get { return this._DisplayRowsCount; }
}
//是否最新
protected bool _IsNew = false;
public bool IsNew
{
set { this._IsNew = value; }
get { return this._IsNew; }
}
//字符串长度
protected int _WordsCount = 18;
public int WordsCount
{
set { this._WordsCount = value; }
get { return this._WordsCount; }
}
//每行显示的样式(left,center,right)
protected string _ItemAlign = "left";
public string ItemAlign
{
set { this._ItemAlign = value; }
get { return this._ItemAlign; }
}
//每页显示多少
protected int _PageSize;
public int PageSize
{
set { this._PageSize = value; }
get { return this._PageSize; }
}
//行背景
protected string _ItemStyle;
protected string ItemStyle
{
set { this._ItemStyle = value; }
get { return this._ItemStyle; }
}
//方向
protected string _Direction;
public string Drirecion
{
set { this._Direction = value; }
get { return this._Direction; }
}
//标题高度
protected int _TitleHeight;
public int TitleHeight
{
set { this._TitleHeight = value; }
get { return this._TitleHeight; }
}
//截取后的字符串
public string GetTitle(object title)
{
string str = title.ToString();
return str.Length < _WordsCount ? str : str.Substring(0, _WordsCount) + "..."; }
BJProduct Product = new BJProduct();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ShowProductInfo();
}
}
//显示解决方案类别下的产品信息
protected void ShowProductInfo()
{
Product.Version = "cn";
DataTable dt = WebManager.List(Product, "", "[ID],[Title],[PictureUrl]", "&version=", "[OrderNo]");
DLProductInfo.DataSource = dt;
Ascpager1.DataSource = dt.DefaultView;
DLProductInfo.DataSource = Ascpager1.PageDataSource;
DLProductInfo.DataBind();
}
</script>
<tr>
<td>
<asp:DataList ID="DLProductInfo" runat="server" Width="<%#_ControlWidth %>" RepeatColumns="<%#_DisplayRowsCount %>" RepeatDirection="Horizontal">
<ItemTemplate>
<table width="<%=_ItemHeight %>" border="0" cellpadding="0" cellspacing="0" class="<%=_LinkStyle %>">
<tr>
<td height="<%=_TdHeight %>" align="center"><table width="68%" height="<%=_TableHeight %>" border="0" cellpadding="0" cellspacing="0" class="<%=_TdStyle %>">
<tr>
<td height="<%=_TdHeight2 %>" align="center"><a target="_blank" href="neir.aspx?id=<%#Eval("ID") %>"><img src="<%#Eval("PictureUrl") %>" width="<%=_ImgWidth %>" height="<%=_ImgHeight %>" border="0" onload="javascript:DrawImage(this,<%=_ImgWidth %>,<%=_ImgHeight %>);" /></a></td>
</tr>
</table></td>
</tr>
<tr>
<td height="<%=_TdHeight3 %>" align="center"><a target="_blank" href="neir.aspx?id=<%#Eval("ID") %>"><%#GetTitle(Eval("Title"))%></a></td>
</tr>
</table></ItemTemplate>
</asp:DataList>
</td>
</tr>
<tr>
<td align="center">
<uc1:ascpager ID="Ascpager1" runat="server" PageSize="<%#_PageSize %>" />
</td>
</tr>
</table>
解决方法:
你可以通过一个key来判断实例化哪个角色,所以在xml中只用存储这个key就可以了
private class NodeProps
{
private string _text;
private string _key; [Category("Layout")]
[Description("节点显示的文本值.")]
public string Text
{
get { return _text; }
set { _text = value; }
}
//节点角色关键词
public string Key
{
get { return _key; }
set { Action1 = value; }
} [Description("节点角色值的选择")]
[Category("Behavior")]
public MyAction Action
{
get {
if(Key=="1")
{
return new MyAction("...").
}
else if (Key == "2")
{
return new MyAction("...").
}
...
}
set { Action1 = value; }
}
这是这么原因啊
NodeProps 是我自定义属性的类
要学会把大问题分成小问题,逐个解决小问题就容易多了。建议你先这样试验:
1,新建一个Winform项目,
2,拉一个PropertyGrid和一个Button进去,双击Button
3,添加给出例子代码
4,帮定NodeProps实例到PropertyGrid:
public Form1()
{
InitializeComponent();
this.propertyGrid1.SelectedObject = new NodeProps();
}
设置完成后下来在生成一个xml文件