集合编辑器属性保存值的问题
我写的个组件,有以下一个属性printRow的属性是我自定义的一个CollectionBase的集合,在用这组件时,设置printRow属性时正确地弹出集合编辑器,可以添加删除编辑,但一确定了再进去却根本没有保存到值,当然在代码里面看的值也是空的。我查了下资料,应该是要“指定特定的设计器序列化策略”,但没找到示例代码,哪位高人帮帮忙
private printCollection _printRow;
[Category( "列设置 "),
Description( "有关打印列的设置 ")]
/// <summary>
/// 有关打印列的设置
/// </summary>
public printCollection printRow
{
get { return _printRow; }
set { _printRow = value; }
}
[解决办法]
up
[解决办法]
帮LZ顶
[解决办法]
首先你要设定PersistenceMode(PersistenceMode.InnerProperty)
也就是
private printCollection _printRow;
[Category( "列设置 "),
Description( "有关打印列的设置 "),
PersistenceMode(PersistenceMode.InnerProperty)]
/// <summary>
/// 有关打印列的设置
/// </summary>
public printCollection printRow
{
get { return _printRow; }
set { _printRow = value; }
}
这样IDE才会把你的printCollection里的值保存下来
如果你是用系统已有的集合,只要添加TypeConverter(typeof(转换器名))就可以了
如果你是自定义的集合属性,还必须定义自已的类型转换类
继承于ExpandableObjectConverter的转换器
以下代码仅共参考:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Drawing.Design;
using System.Web.UI.Design.WebControls;
using System.Collections.Specialized;
using System.Drawing;
using System.Security.Permissions;
using System.Globalization;
using System.ComponentModel.Design;
namespace Wima.Web.UI
{
/// <summary>
/// 边距
/// </summary>
[TypeConverter(typeof(MarginItemConverter))]
public class MarginItem : IStateManager
{
public MarginItem()
{
}
/// <summary>
/// 左边距
/// </summary>
[Category( "Behavior "), Description( "左边距 "), NotifyParentProperty(true)]
public Unit Left
{
get
{
object o = ViewState[ "Left "];
if (o != null)
{
return (Unit)o;
}
return Unit.Parse( "30 ");
}
set
{
ViewState[ "Left "] = value;
}
}
/// <summary>
/// 右边距
/// </summary>
[Category( "Behavior "), Description( "右边距 "), NotifyParentProperty(true)]
public Unit Right
{
get
{
object o = ViewState[ "Right "];
if (o != null)
{
return (Unit)o;
}
return Unit.Parse( "60 ");
}
set
{
ViewState[ "Right "] = value;
}
}
/// <summary>
/// 上边距
/// </summary>
[Category( "Behavior "), Description( "上边距 "), NotifyParentProperty(true)]
public Unit Top
{
get
{
object o = ViewState[ "Top "];
if (o != null)
{
return (Unit)o;
}
return Unit.Parse( "30 ");
}
set
{
ViewState[ "Top "] = value;
}
}
/// <summary>
/// 底边距
/// </summary>
[Category( "Behavior "), Description( "底边距 "), NotifyParentProperty(true)]
public Unit Bottom
{
get
{
object o = ViewState[ "Bottom "];
if (o != null)
{
return (Unit)o;
}
return Unit.Parse( "30 ");
}
set
{
ViewState[ "Bottom "] = value;
}
}
#region "IStateManager接口 "
private static bool _isTrackingViewState;
private static StateBag _viewState;
static StateBag ViewState
{
get
{
if (_viewState == null)
{
_viewState = new StateBag(false);
if (_isTrackingViewState) ((IStateManager)_viewState).TrackViewState();
}
return _viewState;
}
}
bool IStateManager.IsTrackingViewState
{
get
{
return _isTrackingViewState;
}
}
void IStateManager.LoadViewState(object savedState)
{
if (savedState != null)
{
((IStateManager)ViewState).LoadViewState(savedState);
}
}
object IStateManager.SaveViewState()
{
object savedState = null;
if (_viewState != null)
{
savedState = ((IStateManager)_viewState).SaveViewState();
}
return savedState;
}
void IStateManager.TrackViewState()
{
_isTrackingViewState = true;
if (_viewState != null)
{
((IStateManager)_viewState).TrackViewState();
}
}
#endregion
}
public class MarginItemConverter : ExpandableObjectConverter
{
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;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value == null)
{
return new MarginItem();
}
if (value is string)
{
string s = (string)value;
if (s.Length == 0)
{
return new MarginItem();
}
return "MarginItem ";
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value != null)
{
if (!(value is MarginItem))
{
throw new ArgumentException(
"错误 ", "value ");
}
}
if (destinationType == typeof(string))
{
if (value == null)
{
return String.Empty;
}
return "MarginItem ";
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
public class MarginItemEditor : CollectionEditor
{
public MarginItemEditor(Type type)
: base(type)
{
}
protected override bool CanSelectMultipleInstances()
{
return false;
}
protected override Type CreateCollectionItemType()
{
return typeof(MarginItem);
}
}
}
[解决办法]
mark