UserControl:数据绑定 [英] UserControl: Databinding

查看:67
本文介绍了UserControl:数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个UserControl包含一些控件,其中一个是ComboBox。

所有InternalControls都是Private,有些是允许的通过公共方法访问。

我想做的事情之一(当然)是设置DataBindings。

我使用的方法显然有问题,因为我不能在Designer中设置属性,DataSource-Property看起来像''ReadOnly'',没有dropdown。使用System.Windows.Forms.ListControl中的列表


Dim _DataSource As Object

< EditorBrowsable(EditorBrowsableState.Always),_

可浏览(真),_

描述("下拉列表的数据源。),类别(" Data")> _

Public Overridable Property DataSource()As Object

获取

返回_DataSource

结束获取

Set(ByVal Value As Object)

_DataSource = Value

Me.Internal_UiComboBox.DataSource = Value

End Set
结束财产


Dim _DisplayMember As String

< EditorBrowsable(EditorBrowsableState.Always),_

可浏览(True),_

描述("指定要显示其内容的数据源的属性。),Category(" Data")> _

Public Overridable Property DisplayMember()As String

获取

返回_DisplayMember

结束获取

Set(ByVal Value As String)

_DisplayMember = Value

Me.Internal_UiComboBox.DisplayMember = Value

End Set
结束属性

在高级向导中我只能设置一个值,我不能设置数据集,只能设置一个DataSet.DataTable.DataColumn。


我的方法出了什么问题?


TIA,


Michael

Hi,

I have a UserControl containing some controls of which one is a ComboBox.
All the InternalControls are Private and some are allowed to be accessed through Public Methods.
One of the things I would like to do (of course) is set the DataBindings.
Something clearly is wrong with the approach I use, because I can''t set the Properties in the Designer, the DataSource-Property appears like ''ReadOnly'', there is no "dropdown" with the list from System.Windows.Forms.ListControl

Dim _DataSource As Object
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("The data source for the dropdown list."), Category("Data")> _
Public Overridable Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

Dim _DisplayMember As String
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("Specifies the property of the data source whose content you want to display."), Category("Data")> _
Public Overridable Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
Me.Internal_UiComboBox.DisplayMember = Value
End Set
End Property
In the Advanced Wizard I can Set a Value, only, I can''t set a DataSet, only a DataSet.DataTable.DataColumn.

What is wrong with my approach?

TIA,

Michael

推荐答案

我只是在这里猜测,所以请把它当作它的价值;您可以尝试绑定到某些IEnumerable或其他接口对象而不是使用对象吗?这样您就知道数据源是受支持的,并且设计者知道期望的对象类型。我不知道IEnumerable是否适合您,但它可以在这个例子中使用。


Dim DataSource as IEnumerable


Public Overridable Property DataSource()as IEnumerable

获取

返回_DataSource

结束获取

Set(ByVal Value As IEnumerable)

_DataSource = Value

Me.Internal_UiComboBox.DataSource = Value

结束集

结束财产


在你的项目中你可以做一些像


Dim ds作为新数据集

ds.ReadXml(" C :\Path_to_some_xml_file\MyFile.xml")

'' 的DataSet那并不支持IEnumerable的,数据视图确实

Me.MyUserConrol.DataSource = ds.Tables(0 ).DefaultView

Me.MyUserControl.DisplayMember =" SomeProperty"


- 或 -


设计器将显示所有IEnumerable接口,并允许您选择将与y一起使用的数据源我们的对象。


我在对象浏览器中查找了组合框,看起来它支持IList,IEnumerable和ICollection。 IList和ICollection都实现了IEnumerable。


希望这会有所帮助。

Jared

" Michael Maes" < MI ***** @ merlot.com>在留言新闻中写道:u9 ************** @ TK2MSFTNGP12.phx.gbl ...




我有一个UserControl包含一些控件,其中一个是ComboBox。

所有InternalControl都是私有的,有些可以通过公共方法访问。

其中一个我想做的事情(当然)是设置DataBindings。

我使用的方法显然有问题,因为我不能在Designer中设置属性,DataSource-Property看起来像''ReadOnly'',没有下拉字样。使用System.Windows.Forms.ListControl中的列表


Dim _DataSource As Object

< EditorBrowsable(EditorBrowsableState.Always),_

可浏览(真),_

描述("下拉列表的数据源。),类别(" Data")> _

Public Overridable Property DataSource()As Object

获取

返回_DataSource

结束获取

Set(ByVal Value As Object)

_DataSource = Value

Me.Internal_UiComboBox.DataSource = Value

End Set
结束财产


Dim _DisplayMember As String

< EditorBrowsable(EditorBrowsableState.Always),_

可浏览(True),_

描述("指定要显示其内容的数据源的属性。),Category(" Data")> _

Public Overridable Property DisplayMember()As String

获取

返回_DisplayMember

结束获取

Set(ByVal Value As String)

_DisplayMember = Value

Me.Internal_UiComboBox.DisplayMember = Value

End Set
结束属性

在高级向导中我只能设置一个值,我不能设置数据集,只能设置一个DataSet.DataTable.DataColumn。


我的方法出了什么问题?


TIA,


Michael
I''m just guessing here, so take this for what it''s worth; instead of using an object could you try to bind to some IEnumerable or other interface object? That way you know the datasource is supported and the designer know what type(s) of objects to expect. I don''t know if IEnumerable is the right interface for you but it will work in this example.

Dim DataSource as IEnumerable

Public Overridable Property DataSource() As IEnumerable
Get
Return _DataSource
End Get
Set(ByVal Value As IEnumerable)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

in your project you could do something like

Dim ds As New DataSet
ds.ReadXml("C:\Path_to_some_xml_file\MyFile.xml")
''DataSet doesn''t support IEnumerable, DataView does
Me.MyUserConrol.DataSource = ds.Tables(0).DefaultView
Me.MyUserControl.DisplayMember = "SomeProperty"

-- or --

The designer will display all IEnumerable interfaces and allow you to choose a datasource that will work with your object.

I looked up the combo box in the Object browser, it looks like it supports IList, IEnumerable and ICollection. Both IList and ICollection implement IEnumerable.

Hope this helps.
Jared
"Michael Maes" <mi*****@merlot.com> wrote in message news:u9**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a UserControl containing some controls of which one is a ComboBox.
All the InternalControls are Private and some are allowed to be accessed through Public Methods.
One of the things I would like to do (of course) is set the DataBindings.
Something clearly is wrong with the approach I use, because I can''t set the Properties in the Designer, the DataSource-Property appears like ''ReadOnly'', there is no "dropdown" with the list from System.Windows.Forms.ListControl

Dim _DataSource As Object
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("The data source for the dropdown list."), Category("Data")> _
Public Overridable Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

Dim _DisplayMember As String
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("Specifies the property of the data source whose content you want to display."), Category("Data")> _
Public Overridable Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
Me.Internal_UiComboBox.DisplayMember = Value
End Set
End Property
In the Advanced Wizard I can Set a Value, only, I can''t set a DataSet, only a DataSet.DataTable.DataColumn.

What is wrong with my approach?

TIA,

Michael


嗨Jared,


感谢您的回复。

这不会成功,因为我希望我的控制能够绑定到ILists和ICollections。因为我希望能够设置Datasource&除了Datamember之外,在这种情况下我不能绑定到DataSource。


我希望我的绑定设置如下:(在属性设计器中和运行时一样)


使用UC

.DataSource = DataSource(例如DataSet) - >此不会工作

.DataMember = QUOT;数据表"

.DisplayMember = QUOT;柱"

.ValueMember = QUOT;柱" < br $> b $ b结束


问候,


Michael


"贾里德" < VB *********** @ email.com>在留言新闻中写道:10 ************* @ corp.supernews.com ...

我只是在这里猜测,所以请把它拿出去吧的价值;您可以尝试绑定到某些IEnumerable或其他接口对象而不是使用对象吗?这样您就知道数据源是受支持的,并且设计者知道期望的对象类型。我不知道IEnumerable是否适合您,但它可以在这个例子中使用。


Dim DataSource as IEnumerable


Public Overridable Property DataSource()as IEnumerable

获取

返回_DataSource

结束获取

Set(ByVal Value As IEnumerable)

_DataSource = Value

Me.Internal_UiComboBox.DataSource = Value

结束集

结束财产


在你的项目中你可以做一些像


Dim ds作为新数据集

ds.ReadXml(" C :\Path_to_some_xml_file\MyFile.xml")

'' 的DataSet那并不支持IEnumerable的,数据视图确实

Me.MyUserConrol.DataSource = ds.Tables(0 ).DefaultView

Me.MyUserControl.DisplayMember =" SomeProperty"


- 或 -


设计器将显示所有IEnumerable接口,并允许您选择将与y一起使用的数据源我们的对象。


我在对象浏览器中查找了组合框,看起来它支持IList,IEnumerable和ICollection。 IList和ICollection都实现了IEnumerable。


希望这会有所帮助。

Jared

" Michael Maes" < MI ***** @ merlot.com>在留言新闻中写道:u9 ************** @ TK2MSFTNGP12.phx.gbl ...




我有一个UserControl包含一些控件,其中一个是ComboBox。

所有InternalControl都是私有的,有些可以通过公共方法访问。

其中一个我想做的事情(当然)是设置DataBindings。

我使用的方法显然有问题,因为我不能在Designer中设置属性,DataSource-Property看起来像''ReadOnly'',没有下拉字样。使用System.Windows.Forms.ListControl中的列表


Dim _DataSource As Object

< EditorBrowsable(EditorBrowsableState.Always),_

可浏览(真),_

描述("下拉列表的数据源。),类别(" Data")> _

Public Overridable Property DataSource()As Object

获取

返回_DataSource

结束获取

Set(ByVal Value As Object)

_DataSource = Value

Me.Internal_UiComboBox.DataSource = Value

End Set
结束财产


Dim _DisplayMember As String

< EditorBrowsable(EditorBrowsableState.Always),_

可浏览(True),_

描述("指定要显示其内容的数据源的属性。),Category(" Data")> _

Public Overridable Property DisplayMember()As String

获取

返回_DisplayMember

结束获取

Set(ByVal Value As String)

_DisplayMember = Value

Me.Internal_UiComboBox.DisplayMember = Value

End Set
结束属性

在高级向导中我只能设置一个值,我不能设置数据集,只能设置一个DataSet.DataTable.DataColumn。


我的方法出了什么问题?


TIA,


Michael
Hi Jared,

Thanks for your reply.
This won''t do the trick because I want my control to be able to bind to ILists and ICollections. As I want to be able to set the Datasource & the Datamember apart, I can''t bind to a DataSource in this case.

I want my bindings set like: (both in the Property-Designer as in Run--Time)

With UC
.DataSource = DataSource (eg DataSet) --> This wont work.
.DataMember = "DataTable"
.DisplayMember = "Column"
.ValueMember = "Column"
End With

Regards,

Michael

"Jared" <VB***********@email.com> wrote in message news:10*************@corp.supernews.com...
I''m just guessing here, so take this for what it''s worth; instead of using an object could you try to bind to some IEnumerable or other interface object? That way you know the datasource is supported and the designer know what type(s) of objects to expect. I don''t know if IEnumerable is the right interface for you but it will work in this example.

Dim DataSource as IEnumerable

Public Overridable Property DataSource() As IEnumerable
Get
Return _DataSource
End Get
Set(ByVal Value As IEnumerable)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

in your project you could do something like

Dim ds As New DataSet
ds.ReadXml("C:\Path_to_some_xml_file\MyFile.xml")
''DataSet doesn''t support IEnumerable, DataView does
Me.MyUserConrol.DataSource = ds.Tables(0).DefaultView
Me.MyUserControl.DisplayMember = "SomeProperty"

-- or --

The designer will display all IEnumerable interfaces and allow you to choose a datasource that will work with your object.

I looked up the combo box in the Object browser, it looks like it supports IList, IEnumerable and ICollection. Both IList and ICollection implement IEnumerable.

Hope this helps.
Jared
"Michael Maes" <mi*****@merlot.com> wrote in message news:u9**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a UserControl containing some controls of which one is a ComboBox.
All the InternalControls are Private and some are allowed to be accessed through Public Methods.
One of the things I would like to do (of course) is set the DataBindings.
Something clearly is wrong with the approach I use, because I can''t set the Properties in the Designer, the DataSource-Property appears like ''ReadOnly'', there is no "dropdown" with the list from System.Windows.Forms.ListControl

Dim _DataSource As Object
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("The data source for the dropdown list."), Category("Data")> _
Public Overridable Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

Dim _DisplayMember As String
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("Specifies the property of the data source whose content you want to display."), Category("Data")> _
Public Overridable Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
Me.Internal_UiComboBox.DisplayMember = Value
End Set
End Property
In the Advanced Wizard I can Set a Value, only, I can''t set a DataSet, only a DataSet.DataTable.DataColumn.

What is wrong with my approach?

TIA,

Michael


Michael,

原谅我的无知,但是,设计师不仅支持强类型吗?现在我很想知道答案。

Jared

Michael Maes < MI ***** @ merlot.com>在留言新闻中写道:%2 **************** @ tk2msftngp13.phx.gbl ...

嗨Jared,

感谢您的回复。

这不会成功,因为我希望我的控件能够绑定到IList和ICollections。因为我希望能够设置Datasource&除了Datamember之外,在这种情况下我不能绑定到DataSource。


我希望我的绑定设置如下:(在属性设计器中和运行时一样)


使用UC

.DataSource = DataSource(例如DataSet) - >这不行。

.DataMember =" DataTable"

.DisplayMember =" Column"

.ValueMember =" Column" < br $> b $ b结束


问候,


Michael


"贾里德" < VB *********** @ email.com>在留言新闻中写道:10 ************* @ corp.supernews.com ...

我只是在这里猜测,所以请把它拿出去吧的价值;您可以尝试绑定到某些IEnumerable或其他接口对象而不是使用对象吗?这样您就知道数据源是受支持的,并且设计者知道期望的对象类型。我不知道IEnumerable是否适合您,但它可以在这个例子中使用。


Dim DataSource as IEnumerable


Public Overridable Property DataSource()as IEnumerable

获取

返回_DataSource

结束获取

Set(ByVal Value As IEnumerable)

_DataSource = Value

Me.Internal_UiComboBox.DataSource = Value

结束集

结束财产


在你的项目中你可以做一些像


Dim ds作为新数据集

ds.ReadXml(" C :\Path_to_some_xml_file\MyFile.xml")

'' 的DataSet那并不支持IEnumerable的,数据视图确实

Me.MyUserConrol.DataSource = ds.Tables(0 ).DefaultView

Me.MyUserControl.DisplayMember =" SomeProperty"


- 或 -


设计器将显示所有IEnumerable接口,并允许您选择将与y一起使用的数据源我们的对象。


我在对象浏览器中查找了组合框,看起来它支持IList,IEnumerable和ICollection。 IList和ICollection都实现了IEnumerable。


希望这会有所帮助。

Jared

" Michael Maes" < MI ***** @ merlot.com>在留言新闻中写道:u9 ************** @ TK2MSFTNGP12.phx.gbl ...




我有一个UserControl包含一些控件,其中一个是ComboBox。

所有InternalControl都是私有的,有些可以通过公共方法访问。

其中一个我想做的事情(当然)是设置DataBindings。

我使用的方法显然有问题,因为我不能在Designer中设置属性,DataSource-Property看起来像''ReadOnly'',没有下拉字样。使用System.Windows.Forms.ListControl中的列表


Dim _DataSource As Object

< EditorBrowsable(EditorBrowsableState.Always),_

可浏览(真),_

描述("下拉列表的数据源。),类别(" Data")> _

Public Overridable Property DataSource()As Object

获取

返回_DataSource

结束获取

Set(ByVal Value As Object)

_DataSource = Value

Me.Internal_UiComboBox.DataSource = Value

End Set
结束财产


Dim _DisplayMember As String

< EditorBrowsable(EditorBrowsableState.Always),_

可浏览(True),_

描述("指定要显示其内容的数据源的属性。),Category(" Data")> _

Public Overridable Property DisplayMember()As String

获取

返回_DisplayMember

结束获取

Set(ByVal Value As String)

_DisplayMember = Value

Me.Internal_UiComboBox.DisplayMember = Value

End Set
结束属性

在高级向导中我只能设置一个值,我不能设置数据集,只能设置一个DataSet.DataTable.DataColumn。


我的方法出了什么问题?


TIA,


Michael
Michael,
Forgive my ignorance, but, doesn''t the designer only support strong types? Now I am curious to see the answer.
Jared
"Michael Maes" <mi*****@merlot.com> wrote in message news:%2****************@tk2msftngp13.phx.gbl...
Hi Jared,

Thanks for your reply.
This won''t do the trick because I want my control to be able to bind to ILists and ICollections. As I want to be able to set the Datasource & the Datamember apart, I can''t bind to a DataSource in this case.

I want my bindings set like: (both in the Property-Designer as in Run--Time)

With UC
.DataSource = DataSource (eg DataSet) --> This wont work.
.DataMember = "DataTable"
.DisplayMember = "Column"
.ValueMember = "Column"
End With

Regards,

Michael

"Jared" <VB***********@email.com> wrote in message news:10*************@corp.supernews.com...
I''m just guessing here, so take this for what it''s worth; instead of using an object could you try to bind to some IEnumerable or other interface object? That way you know the datasource is supported and the designer know what type(s) of objects to expect. I don''t know if IEnumerable is the right interface for you but it will work in this example.

Dim DataSource as IEnumerable

Public Overridable Property DataSource() As IEnumerable
Get
Return _DataSource
End Get
Set(ByVal Value As IEnumerable)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

in your project you could do something like

Dim ds As New DataSet
ds.ReadXml("C:\Path_to_some_xml_file\MyFile.xml")
''DataSet doesn''t support IEnumerable, DataView does
Me.MyUserConrol.DataSource = ds.Tables(0).DefaultView
Me.MyUserControl.DisplayMember = "SomeProperty"

-- or --

The designer will display all IEnumerable interfaces and allow you to choose a datasource that will work with your object.

I looked up the combo box in the Object browser, it looks like it supports IList, IEnumerable and ICollection. Both IList and ICollection implement IEnumerable.

Hope this helps.
Jared
"Michael Maes" <mi*****@merlot.com> wrote in message news:u9**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a UserControl containing some controls of which one is a ComboBox.
All the InternalControls are Private and some are allowed to be accessed through Public Methods.
One of the things I would like to do (of course) is set the DataBindings.
Something clearly is wrong with the approach I use, because I can''t set the Properties in the Designer, the DataSource-Property appears like ''ReadOnly'', there is no "dropdown" with the list from System.Windows.Forms.ListControl

Dim _DataSource As Object
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("The data source for the dropdown list."), Category("Data")> _
Public Overridable Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
_DataSource = Value
Me.Internal_UiComboBox.DataSource = Value
End Set
End Property

Dim _DisplayMember As String
<EditorBrowsable(EditorBrowsableState.Always), _
Browsable(True), _
Description("Specifies the property of the data source whose content you want to display."), Category("Data")> _
Public Overridable Property DisplayMember() As String
Get
Return _DisplayMember
End Get
Set(ByVal Value As String)
_DisplayMember = Value
Me.Internal_UiComboBox.DisplayMember = Value
End Set
End Property
In the Advanced Wizard I can Set a Value, only, I can''t set a DataSet, only a DataSet.DataTable.DataColumn.

What is wrong with my approach?

TIA,

Michael


这篇关于UserControl:数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆