【问题标题】:Property set isn't being triggered with a UITypeEditor implemented实现 UITypeEditor 时不会触发属性集
【发布时间】:2011-06-30 16:52:57
【问题描述】:

我有一个属性网格,当单击某个属性的按钮时,会填充某些字段。但是,不会触发该属性的设置。我不知道为什么。

 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 应用程序。

【问题讨论】:

  • 如果编辑器没有设置属性,我认为它会读取对象,然后对其引用进行操作。您的 dataToSet 应该已经反映了更改...
  • 它没有更新它。当我取出 = new OptoSigmaSettings(); 的初始化时,它将进入函数的“设置”部分。之后……不会了。函数的返回似乎没有按我的需要工作。

标签: c# .net winforms propertygrid uitypeeditor


【解决方案1】:

问题在于您的编辑器返回完全相同的引用(您获得了 opto 并返回了 opto)。所以即使修改了 opto 的一些内部属性,opto ref 也不会改变。如果您绝对需要进入 set 访问器,请在 EditValue 中创建一个新的 OptoSigmaSettings 并使用表单返回的内容修改其属性。请注意,我没有在您的代码中看到如何使用现有 opto 的内容初始化表单。

PS:我刚刚看到你上面的评论。请注意,如果您不初始化 dataToGet,则它为 null,这就是它第一次工作的原因(null 与表单返回的值不同)。

注 2:Marino 说得对,即使你的集合没有被调用,你的对象的属性仍然会更新(Direction、FunctionToCall 和 Duration)。

【讨论】:

    【解决方案2】:

    最后的解决办法是这样的:

     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;
        }
    

    【讨论】:

      【解决方案3】:

      我已经有一段时间没有使用属性网格了,但这是我的 2cents。

      这里没有在您创建的 DataToGet 子类上设置 DataToGet 属性。

      在您的代码中:

      OptoSigmaSettings opto = 作为 OptoSigmaSettings 的值;

      看起来缺少的是将值转换为 DataToGet,然后设置其 DataToGet 属性:

      DataToGet opto = 作为 DataToGet 的值; opto.DataToGet=myobject;

      【讨论】:

      • 好吧,我这样做:OptoSigmaSettings opto = value as OptoSigmaSettings;在 OptoSetupFormEditor 的 Edit value 函数中。我至少应该看到弹出消息框吗?
      • 没有。您正在创建一个局部变量并将该变量的局部值分配给值转换。您没有访问属性。另外,正如我之前所说,您将值转换为 optoSigmaSettings,它没有属性,而不是 DataToGet。
      猜你喜欢
      • 1970-01-01
      • 2021-01-27
      • 1970-01-01
      • 2014-07-20
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多