2012-07-09 56 views
2

我已经创建了一个具有自定义数组值的属性网格。当用户选择其中一个下拉列表时,我希望它显示一个表单。我的问题不是它不起作用,它不是活动过度,并且尽管只声明了一次,但显示了大约6次的形式。如果我选择ShowDialog,它将显示两次窗体,并在尝试关闭第二个对话框时会创建另外两个窗体实例。以下是我正在使用的代码。我无法弄清楚什么是错的。C#属性多次显示表单的属性选择

//Property Grid Type 
internal class TransferConnectionPropertyConverter : StringConverter 
    { 
     public override bool GetStandardValuesSupported(ITypeDescriptorContext context) 
     { 
      return true; 
     } 

     public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) 
     { 
      return true; 
     } 

     public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) 
     { 
      return new StandardValuesCollection(new string[] { "", NEW_CONN }); 
     }    
    } 

//Property Grid Node 
[Category("Connection"), 
Description("Specifies the type of connection for this task.")] 
[TypeConverter(typeof(TransferConnectionPropertyConverter))] 
public string TransferProtocol 
{ 
    get 
    { 
     if (stConnectionType == NEW_CONN) 
     { 
       ConnectionDetailsForm connDetails = new ConnectionDetailsForm(); 
       connDetails.Show();       
     } 
     return stConnectionType; 
    } 
    set 
    { 
     stConnectionType = value; 
    }          
} 

回答

1

你需要为一个编辑器,你肯定不希望呈现出财产get property期间的形式,因为这可能PropertyGrid中的生命周期中被调用多次。

简单类(从this example找到):

public class StringEditor : UITypeEditor { 
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { 
    return UITypeEditorEditStyle.Modal; 
    } 

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { 
    IWindowsFormsEditorService svc = (IWindowsFormsEditorService) 
     provider.GetService(typeof(IWindowsFormsEditorService)); 
    if (svc != null) { 
     svc.ShowDialog(new ConnectionDetailsForm()); 
     // update etc 
    } 
    return value; 
    } 
} 

然后你装饰你的财产这个编辑器(和记,我删除了转换器,因为你的财产只是一个字符串,没什么可转换):

[Category("Connection")] 
[Description("Specifies the type of connection for this task.")] 
[Editor(typeof(StringEditor), typeof(UITypeEditor))] 
public string TransferProtocol { 
    get { 
    return stConnectionType; 
    } 
    set { 
    stConnectionType = value; 
    } 
} 
+0

你说得对。我在错误的地方打电话给表单表示,把它放在获得是它多次显示的原因。 '哦' - 只是没有想到。我改变了它,并将其放置在按我预期工作的集合中。 – zeencat 2012-07-09 13:23:54