我想连接一个BindingSource类对象列表,然后将对象值连接到一个ComboBox.
谁能建议怎么做?
public class Country
{
public string Name { get; set; }
public IList<City> Cities { get; set; }
public Country()
{
Cities = new List<City>();
}
}
Run Code Online (Sandbox Code Playgroud)
是我的类,我想将其name字段绑定到BindingSource,然后可以与ComboBox关联
Form有一个Combobox和一个ListBox.单击"添加"按钮时,我想将选定的项目从ComboBox添加到ListBox.
public partial class MyForm:Form
{
List<MyData> data = new List<MyData>();
private void ShowData()
{
listBox1.DataSource = data;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "Id";
}
private void buttonAddData_Click(object sender, EventArgs e)
{
var selection = (MyData)comboBox1.SelectedItem;
data.Add(selection);
ShowData();
}
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,所选项目将替换为ListBox中的新选择.我需要将项目添加到列表中.
我的代码出了什么问题?
我在过去的两年里开发了一些基于Winforms Application的数据,一切正常.此应用程序构建在层(DataAccess,业务逻辑和UI)上.对于Businness Logic,我的所有对象都继承自一个名为BaseEntity的基类,其定义如下(有一些自定义对象和接口,与框架元素结合):
Run Code Online (Sandbox Code Playgroud)Public MustInherit Class BaseEntity Inherits SerializableObject Implements IEntity Implements IComparer, _ IEditableObject, _ INotifyPropertyChanging, INotifyPropertyChanged, _ IApplicationSecurity End Class
在同一个核心库中,我有一个通用的基本集合BaseEntityCollection.这些集合允许我为每个对象定义他的相关强类型集合,这在基于数据的应用程序中是非常有趣的.这是它的基本定义:
Public MustInherit Class BaseEntityCollection(Of T As BaseEntity)
Inherits BindingList(Of T)
Implements IEntityCollection
Implements INotifyPropertyChanged, INotifyPropertyChanging, ICopyable(Of T)
Implements IDisposable
Implements ISerializable
End Class
Run Code Online (Sandbox Code Playgroud)
如您所见,我使用Winforms中正确数据绑定所需的所有内容:
我也对新技术感兴趣,所以我最近看了一些关于WPF的网络直播.在这些网络广播中,它们用作收集和数据绑定支持ObservableCollection(Of T)的基类.
我正在考虑将我的一些应用程序从Winforms迁移到WPF以用于UI层.
我的问题是,对于我的业务逻辑,最好是基于BindingList(Of T)保留我的集合,还是应该更改我的基本集合类以使其继承自ObservableCollection(Of T).我想为我的所有项目保留一个独特的基础集合,可以在Winforms应用程序,WPF应用程序或ASP.NET中使用.我也在我的项目中使用Linq to Objects,所以我没有限制只保留基于框架2.0的项目.
谢谢,
CLABER
我有ComboBox一个DataSource设置为应用程序设置如下
public DetailsForm()
{
InitializeComponent();
this.comboBox1.DataSource = TextSelectionSettings.Default.categories;
}
Run Code Online (Sandbox Code Playgroud)
但我希望用户在运行时需要时向组合框中添加额外的项目。所以我只是在文本框上做了一个简单的点击事件来测试向列表中添加一个新字符串。
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
TextSelectionSettings.Default.categories.Add("test");
TextSelectionSettings.Default.Save();
}
Run Code Online (Sandbox Code Playgroud)
但是ComboBox没有显示我添加到设置中的新字符串。
如何刷新ComboBox以显示设置中的更改?
Refresh() 组合框上的功能不起作用。DataSource再次设置也不起作用。Item直接添加到ComboBoxusingItems.Add()方法中,因为DataSource已设置。