我有一个属性网格,当单击其中一个属性的按钮时,某些字段会被填充。但是,该属性的set不会被触发。我不知道为什么。
private OptoSigmaSettings dataToGet = new OptoSigmaSettings();
[Editor(typeof(OptoSetupFormEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(ExpandableObjectConverter))]
[Category("Setup")]
public OptoSigmaSettings DataToGet
{
get { return dataToGet; }
set
{
MessageBox.Show("Im here"); //This isnt happening.
dataToGet = value; }
}
[Serializable]
public class OptoSigmaSettings
{
private int duration = 0;
private string direction = "Positive";
private string functionToCall = "Home";
public string FunctionToCall
{
get { return functionToCall; }
set { functionToCall = value; }
}
public int Duration
{
get { return duration; }
set { duration = value; }
}
public string Direction
{
get { return direction; }
set { direction = value; }
}
}
public class OptoSetupFormEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
OptoSigmaSettings opto = value as OptoSigmaSettings;
if (service != null && opto != null)
{
using (OptoSigmaSetup form = new OptoSigmaSetup())
{
DialogResult result;
result = service.ShowDialog(form);
if (result == DialogResult.OK)
{
opto.Direction = form.Direction;
opto.FunctionToCall = form.FunctionToCall;
opto.Duration = form.Duration;
}
}
}
return opto;
}
}这是一个使用标准属性网格的WinForms应用程序。
发布于 2011-07-17 02:02:32
以下是最终的解决方案:
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
OptoSigmaLinearSettings opto = value as OptoSigmaLinearSettings;
opto = (OptoSigmaLinearSettings)value;
if (opto == null)
{
opto = new OptoSigmaLinearSettings();
}
if (service != null)
{
using (OptoSigmaLinearSetup form = new OptoSigmaLinearSetup(opto))
{
DialogResult result;
result = service.ShowDialog(form);
if (result == DialogResult.OK)
{
opto = form.GeneralSettings;
}
}
}
return opto;
}发布于 2011-07-01 04:26:43
问题是你的编辑器返回完全相同的引用(你得到opto,你返回opto)。因此,即使您修改了opto的一些内部属性,opto ref也不会更改。如果您确实需要使用set访问器,可以在EditValue中创建一个新的OptoSigmaSettings,并使用表单返回的内容修改其属性。请注意,在您的代码中,我没有看到表单是如何用现有opto的内容初始化的。
附言:我刚刚看到你上面的评论。注意,如果没有初始化dataToGet,那么它就是null,这就是它第一次工作的原因(null与表单返回的值不同)。
注2: Marino说得对,即使你的set没有被调用,你的对象的属性(Direction,FunctionToCall和Duration)仍然会更新。
发布于 2011-07-01 01:15:22
我已经有一段时间没有使用propertygrid了,但这是我的2cents。
这里没有设置您创建的DataToGet子类的DataToGet属性。
在你的代码中:
OptoSigmaSettings opto =OptoSigmaSettings形式的值;
看起来缺少的是将值转换为DataToGet,然后设置其DataToGet属性:
DataToGet opto =值为DataToGet;opto.DataToGet=myobject;
https://stackoverflow.com/questions/6538293
复制相似问题