用户控件的数据源

时间:2009-01-25 21:00:48

标签: c# .net winforms custom-controls

我正在建立一个用户控件。目前它由一个文本框和一个按钮组成 - 作为一种学习体验。这将用作更有用控制的基础。

我想添加一个DataSource,display member和ValueMember 这是我的数据源代码。它将显示在“属性”编辑器中,但已禁用并显示为灰色。我错过了什么?

    private object MyDataSource;

    [Browsable(true)]
    [TypeConverter("System.Windows.Forms.Design.DataSourceConverter")]
    [System.ComponentModel.Bindable(true)]
    public object DataSource
    {
        get
        {
            return MyDataSource;
        }
        set
        {
            if (MyDataSource != value)
                MyDataSource = value;
        }
    }

2 个答案:

答案 0 :(得分:5)

更简单的选择可能是使用属性提供程序:

[AttributeProvider(typeof(IListSource))]

您可以尝试使用程序集限定名称吗?并指定编辑器?

[TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[Editor("System.Windows.Forms.Design.DataSourceListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]

实际上,只需指定程序集即可将其缩写为版本无关:

[TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design")]
[Editor("System.Windows.Forms.Design.DataSourceListEditor, System.Design", typeof(UITypeEditor))]

答案 1 :(得分:1)

我将ComplexBindingProperties属性添加到Control类中,并将AttributeProvider添加到我的DataSource属性

CACHES = {
  'default': {
    'BACKEND': 'django_redis.cache.RedisCache',
    'LOCATION': 'redis://XXX.YYY.ZZZ.cache.amazonaws.com/0',
    'OPTIONS': {
      'REDIS_CLIENT_CLASS': 'rediscluster.RedisCluster',
      'CONNECTION_POOL_CLASS': 'rediscluster.connection.ClusterConnectionPool',
      'CONNECTION_POOL_KWARGS': {
        'skip_full_coverage_check': True # AWS ElasticCache has disabled CONFIG commands
      }
    }
  }
}

...

[ComplexBindingProperties("DataSource", "DataMember")]
public partial class SomeListControl : UserControl

参考:Apply Attributes in Windows Forms Controls