实现 UITypeEditor 时不会触发属性集
我有一个属性网格,当单击其中一个属性的按钮时,某些字段会被填充。但是,该属性的设置不会被触发。我不知道为什么。
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 应用程序。
I have a property grid that when a button is clicked for one of the properties, certain fields are filled in. However the property's set isn't being triggered. I do not know why.
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;
}
}
This is a WinForms app using the standard property grid.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是你的编辑器返回完全相同的引用(你得到 opto 并返回 opto)。因此,即使您修改了 opto 的某些内部属性,opto ref 也不会改变。如果您确实需要进入设置访问器,请在 EditValue 内创建一个新的 OptoSigmaSettings 并使用表单返回的内容修改其属性。请注意,我在您的代码中没有看到表单如何使用现有选项的内容进行初始化。
PS:我刚刚看到你上面的评论。请注意,如果您没有初始化 dataToGet,那么它就是 null,这就是它第一次工作的原因(null 与表单返回的值不同)。
注 2:Marino 说得对,即使你的集合没有被调用,你的对象的属性仍然会更新(Direction、FunctionToCall 和 Duration)。
The problem is that your editor returns exactly the same reference (you get opto and you return opto). So even if you modify some internal properties of opto, the opto ref does not change. If you absolutely need to go in your set accessor, inside EditValue create a new OptoSigmaSettings and modify its properties with what your form returns. Note that I don't see in your code how the form is initialized with the content of the existing opto.
PS: I just saw your comment above. Note that if you don't initialize your dataToGet, then it is null, and that's why it works a first time (null is different than the value returned by your form).
Note 2: Marino is right by saying that even if your set is not called, the properties of your object are still updated (Direction, FunctionToCall and Duration).
最终的解决方案如下:
Here was the solution in the end:
我已经有一段时间没有使用 propertygrid 了,但这是我的 2 美分。
这里没有设置您创建的 DataToGet 子类的 DataToGet 属性。
在您的代码中:
OptoSigmaSettings opto = value as OptoSigmaSettings;
看起来缺少的是将 value 转换为 DataToGet,然后设置其 DataToGet 属性:
DataToGet opto = value as DataToGet;
opto.DataToGet=myobject;
Its been a while since I've used a propertygrid, but here's my 2cents.
Nothing here is setting the DataToGet property on the DataToGet subclass you have created.
In your code:
OptoSigmaSettings opto = value as OptoSigmaSettings;
What looks like is missing is casting value to DataToGet and then setting its DataToGet property:
DataToGet opto = value as DataToGet;
opto.DataToGet=myobject;