.net 数据绑定组合选择编辑项不';好像不行
我正在尝试将组合绑定到对象集合:.net 数据绑定组合选择编辑项不';好像不行,.net,vb.net,winforms,data-binding,.net,Vb.net,Winforms,Data Binding,我正在尝试将组合绑定到对象集合: Dim t As New TradeOrderStatus() Dim ts As List(Of TradeOrderStatus) = t.GetStatuses With Me.cboTradeStatus .DataSource = ts .SelectedItem = Nothing End With 这很好,我看到了组合中的项目列表。但是,当我尝试将组合的SelectedItem设置
Dim t As New TradeOrderStatus()
Dim ts As List(Of TradeOrderStatus) = t.GetStatuses
With Me.cboTradeStatus
.DataSource = ts
.SelectedItem = Nothing
End With
这很好,我看到了组合中的项目列表。但是,当我尝试将组合的SelectedItem设置为以下项目之一时:
Me.cboTradeStatus.SelectedItem = Trade.TradeStatus
什么也没发生。Trade.TradeStatus是TradeOrderStatus类的一个实例,所有必要的部件状态都在下拉列表中可见。SelectedItem保留为Nothing(如果我在绑定代码中省略了.SelectedItem=Nothing行,则它将作为列表中的第一项)
也返回false。有人能帮忙吗
Trade.TradeStatus是TradeOrderStatus类的一个实例
你提到这是一门课;它是否与数据绑定列表中的实例相同?它需要找到一个相等的匹配项。或者,您可以覆盖Equals
(和GetHashCode()
——始终保持两者同步)以实现相同的效果
(编辑) 修复它的最简单方法是绑定到
SelectedValue
;对于“完整”示例(如下),类似于:
cbo.DisplayMember = cbo.ValueMember = "Name";
...
btn.Click += delegate { cbo.SelectedValue = GetCurrentStatus().Name; };
(编辑)
下面是一个C#示例(对不起,我的VB fu很弱),它提供了不同状态的自定义相等性-请注意“uncomment to fix”:
使用System.Windows.Forms;
使用System.Collections.Generic;
MyStatus类{
公共MyStatus(字符串名){name=name;}
公共字符串名称{get;private set;}
公共重写字符串ToString(){return Name;}
/*取消注释以修复
公共覆盖布尔等于(对象对象对象){
MyStatus other=obj作为MyStatus;
返回other!=null&&other.Name==this.Name;
}
public override int GetHashCode(){return Name.GetHashCode();}
*/
}
静态类程序{
静态void Main(){
ComboBox cbo=新ComboBox();
cbo.DataSource=GetStatuses();
按钮btn=新按钮();
btn.Click+=delegate{cbo.SelectedItem=GetCurrentStatus();};
btn.Text=“设置状态”;
btn.Dock=DockStyle.Bottom;
Run(新表单{Controls={cbo,btn});
}
静态列表GetStatuses(){
List stats=新列表();
添加(新MyStatus(“打开”);
添加(新MyStatus(“待定”);
添加(新MyStatus(“已关闭”);
返回统计;
}
静态MyStatus GetCurrentStatus(){
返回新MyStatus(“已关闭”);
}
}
Trade.TradeStatus是TradeOrderStatus类的一个实例
你提到这是一门课;它是否与数据绑定列表中的实例相同?它需要找到一个相等的匹配项。或者,您可以覆盖Equals
(和GetHashCode()
——始终保持两者同步)以实现相同的效果
(编辑) 修复它的最简单方法是绑定到
SelectedValue
;对于“完整”示例(如下),类似于:
cbo.DisplayMember = cbo.ValueMember = "Name";
...
btn.Click += delegate { cbo.SelectedValue = GetCurrentStatus().Name; };
(编辑)
下面是一个C#示例(对不起,我的VB fu很弱),它提供了不同状态的自定义相等性-请注意“uncomment to fix”:
使用System.Windows.Forms;
使用System.Collections.Generic;
MyStatus类{
公共MyStatus(字符串名){name=name;}
公共字符串名称{get;private set;}
公共重写字符串ToString(){return Name;}
/*取消注释以修复
公共覆盖布尔等于(对象对象对象){
MyStatus other=obj作为MyStatus;
返回other!=null&&other.Name==this.Name;
}
public override int GetHashCode(){return Name.GetHashCode();}
*/
}
静态类程序{
静态void Main(){
ComboBox cbo=新ComboBox();
cbo.DataSource=GetStatuses();
按钮btn=新按钮();
btn.Click+=delegate{cbo.SelectedItem=GetCurrentStatus();};
btn.Text=“设置状态”;
btn.Dock=DockStyle.Bottom;
Run(新表单{Controls={cbo,btn});
}
静态列表GetStatuses(){
List stats=新列表();
添加(新MyStatus(“打开”);
添加(新MyStatus(“待定”);
添加(新MyStatus(“已关闭”);
返回统计;
}
静态MyStatus GetCurrentStatus(){
返回新MyStatus(“已关闭”);
}
}
根据马克的回答回答我自己的问题。我已将给定的示例转换为VB等效项:
Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
' Check for null values and compare run-time types.
If obj Is Nothing Or Not Me.GetType() Is obj.GetType() Then
Return False
End If
Dim t As TradeOrderStatus = CType(obj, TradeOrderStatus)
Return Me.ID = t.ID
End Function
Public Overrides Function GetHashCode() As Integer
Return Me.GetHashCode()
End Function
我的组合绑定现在按预期工作。根据Marc的回答回答我自己的问题。我已将给定的示例转换为VB等效项:
Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
' Check for null values and compare run-time types.
If obj Is Nothing Or Not Me.GetType() Is obj.GetType() Then
Return False
End If
Dim t As TradeOrderStatus = CType(obj, TradeOrderStatus)
Return Me.ID = t.ID
End Function
Public Overrides Function GetHashCode() As Integer
Return Me.GetHashCode()
End Function
我的组合绑定现在可以按预期工作。当我在C中使用上面的GetHashCode()解决方案时,我从无限循环中得到一个堆栈溢出:
public override int GetHashCode()
{
return this.GetHashCode();
}
你的意思是说:
return this.Id;
当我做出这个改变时,它起了作用。这真是一件痛苦的事情,下面是C语言中的代码,适用于任何拥有空数据源并且需要覆盖ComboBox中的GetHashCode和Equals方法的人:
using System.Windows.Forms;
namespace Ace.Docs.UI.ComboBoxes
{
internal class DisplayInfo : ComboBox
{
#region Properties (2)
public string DisplayName { get; set; }
public int Id { get; set; }
#endregion Properties
#region Methods (2)
// Public Methods (2)
public override bool Equals(object obj)
{
// Check for null values and compare run-time types.
if (obj == null | (!object.ReferenceEquals(this.GetType(), obj.GetType())))
{
return false;
}
var t = (DisplayInfo)obj;
return this.Id == t.Id;
}
public override int GetHashCode()
{
return this.Id;
}
#endregion Methods
}
}
当我在C#中使用上面的GetHashCode()解决方案时,我从无限循环中得到一个堆栈溢出:
public override int GetHashCode()
{
return this.GetHashCode();
}
你的意思是说:
return this.Id;
当我做出这个改变时,它起了作用。这真是一件痛苦的事情,下面是C语言中的代码,适用于任何拥有空数据源并且需要覆盖ComboBox中的GetHashCode和Equals方法的人:
using System.Windows.Forms;
namespace Ace.Docs.UI.ComboBoxes
{
internal class DisplayInfo : ComboBox
{
#region Properties (2)
public string DisplayName { get; set; }
public int Id { get; set; }
#endregion Properties
#region Methods (2)
// Public Methods (2)
public override bool Equals(object obj)
{
// Check for null values and compare run-time types.
if (obj == null | (!object.ReferenceEquals(this.GetType(), obj.GetType())))
{
return false;
}
var t = (DisplayInfo)obj;
return this.Id == t.Id;
}
public override int GetHashCode()
{
return this.Id;
}
#endregion Methods
}
}
不,这是另一个例子。我试图设置为SelectedItem的一个被实例化为父类的属性,组合中的那些被实例化纯粹是为了填充组合。不,它是一个不同的实例。我试图设置为SelectedItem的对象被实例化为父类的属性,组合中的对象被实例化纯粹是为了填充组合。