DataSource for User Control

故事扮演 提交于 2019-12-09 10:01:46

问题


I am buidling a user control. Currently it consists of a textbox and a button - as a learning experience. This will be used as a basis for a more useful control.

I want to add a DataSource, display member and ValueMember.
Here is my code for the datasource. It will display in the Properties editor, but is disabled and grayed out. What am I missing?

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

回答1:


An easier option may be to use an attribute provider instead:

[AttributeProvider(typeof(IListSource))]

You could try using the assembly-qualified name? and specifying the editor?

[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")]

Actually, you can abbreviate this to be version-independent just by specifying the assembly:

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



回答2:


I added ComplexBindingProperties attribute to my Control class and added AttributeProvider to my DataSource property

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

...

[Category("Data")]
[Description("Indicates the source of data for the control.")]
[RefreshProperties(RefreshProperties.Repaint)]
[AttributeProvider(typeof(IListSource))]
public object DataSource
{
    get { return _ultraGrid.DataSource; }
    set { _ultraGrid.DataSource = value; }
}

[Category("Data")]
[Description("Indicates a sub-list of the data source to show in the control.")]
[Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design", typeof(UITypeEditor))]    
public string DataMember
{
    get
    {
        return _ultraGrid.DataMember;
    }

    set
    {
        _ultraGrid.DataMember = value;
    }
}

Ref: Apply Attributes in Windows Forms Controls



来源:https://stackoverflow.com/questions/478278/datasource-for-user-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!