窗口中的小复杂数据绑定形成组合框 [英] Little complex databinding in windows form combobox
本文介绍了窗口中的小复杂数据绑定形成组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Windows窗体中处理数据绑定时遇到了麻烦。我有两个类,一个是Project,另一个是Update。现在所有项目对象都有一个更新列表,它被绑定到一个组合框,但是当用户更改选择时需要显示/绑定Update对象的属性到另一个控件。但是,当用户更改选择时,不会按预期更新。请帮帮我。
我尝试过:
I am in trouble while working on Databinding in windows form. I have two classes, one is Project and another is Update. Now all project object is having a list of Updates and it is binded to a combobox, but when the user changes selection need to display/bind the properties of Update object to another controls. But is not updating as expected, when the user changes selection. Please help me on this.
What I have tried:
public class Project
{
private int _id;
private string _name;
public Project(int id, string name)
{
_id = id;
_name = name;
ReadUpdates();
}
public List<Update> AvailableUpdates { get; set; }
public int Id { get { return _id; } }
public string ProjName
{
get { return _name; }
set { _name = value; }
}
private void ReadUpdates()
{
AvailableUpdates = new List<Update>();
for (int i = 0; i < 10; i++)
{
AvailableUpdates.Add(new
Update(i, DateTime.Now.AddDays(i)));
}
}
}
public class Update
{
private string _title;
private int _uid;
private DateTime _updatedOn;
public Update(int id, DateTime updatedOn)
{
_title = $"Update:{id}";
_uid = id;
_updatedOn = updatedOn;
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public int UId
{
get { return _uid; }
set { _uid = value; }
}
public DateTime UpdatedOn
{
get { return _updatedOn; }
set { _updatedOn = value; }
}
}
public partial class Form1 : Form
{
private Update _currentUpdate;
private Project _project;
public Form1()
{
InitializeComponent();
_project = new Project(1, "Sample Project");
DoBindings();
}
private void DoBindings()
{
NameBox.DataBindings.Add("Text", _project, "ProjName");
IdBox.DataBindings.Add("Text", _project, "Id");
UpdatesCombo.DataSource = _project.AvailableUpdates;
UpdatesCombo.DisplayMember = "UId";
_currentUpdate = (Update)UpdatesCombo.SelectedItem;
UpdateTitle.DataBindings.Add("Text", _currentUpdate, "Title");
UpdateDate.DataBindings.Add("Value", _currentUpdate, "UpdatedOn");
}
private void UpdatesCombo_SelectionChangeCommitted(object sender, System.EventArgs e)
{
_currentUpdate = (Update)UpdatesCombo.SelectedItem;
}
}
推荐答案
更新:{id};
_uid = id;
_updatedOn = updatedOn;
}
公共字符串标题
{
get {return _title; }
set {_title = value; }
}
public int UId
{
get {return _uid; }
set {_uid = value; }
}
public DateTime UpdatedOn
{
get {return _updatedOn; }
set {_updatedOn = value; }
}
}
公共部分类Form1:表格
{
private Update _currentUpdate;
private Project _project;
public Form1()
{
InitializeComponent();
_project = new Project(1,Sample Project);
DoBindings();
}
private void DoBindings()
{
NameBox.DataBindings.Add(Text,_ project,ProjName);
IdBox.DataBindings.Add(Text,_ project,Id);
UpdatesCombo.DataSource = _project.AvailableUpdates;
UpdatesCombo.DisplayMember =UId;
_currentUpdate =(Update)UpdatesCombo.SelectedItem;
UpdateTitle.DataBindings.Add(Text,_ currentUpdate,Title);
UpdateDate.DataBindings.Add(Value,_ currentUpdate,UpdatedOn);
}
private void UpdatesCombo_SelectionChangeCommitted(object sender,System.EventArgs e)
{
_currentUpdate =(Update)UpdatesCombo.SelectedItem;
}
}
"Update:{id}"; _uid = id; _updatedOn = updatedOn; } public string Title { get { return _title; } set { _title = value; } } public int UId { get { return _uid; } set { _uid = value; } } public DateTime UpdatedOn { get { return _updatedOn; } set { _updatedOn = value; } } } public partial class Form1 : Form { private Update _currentUpdate; private Project _project; public Form1() { InitializeComponent(); _project = new Project(1, "Sample Project"); DoBindings(); } private void DoBindings() { NameBox.DataBindings.Add("Text", _project, "ProjName"); IdBox.DataBindings.Add("Text", _project, "Id"); UpdatesCombo.DataSource = _project.AvailableUpdates; UpdatesCombo.DisplayMember = "UId"; _currentUpdate = (Update)UpdatesCombo.SelectedItem; UpdateTitle.DataBindings.Add("Text", _currentUpdate, "Title"); UpdateDate.DataBindings.Add("Value", _currentUpdate, "UpdatedOn"); } private void UpdatesCombo_SelectionChangeCommitted(object sender, System.EventArgs e) { _currentUpdate = (Update)UpdatesCombo.SelectedItem; } }
使用BindingList< t>而不是List< t>和一个BindingSource,它将按照需要工作
use a BindingList<t> instead of List<t> and a BindingSource and it will work as desired
using System.ComponentModel;
public partial class Form1 : Form
{
private Update _currentUpdate;
private Project _project;
private BindingSource bs;
...
private void DoBindings()
{
NameBox.DataBindings.Add("Text", _project, "ProjName");
IdBox.DataBindings.Add("Text", _project, "Id");
bs = new BindingSource();
bs.DataSource = _project.AvailableUpdates;
UpdatesCombo.DataSource = bs;
UpdatesCombo.DisplayMember = "UId";
UpdatesCombo.ValueMember = "UId";
_currentUpdate = (Update)UpdatesCombo.SelectedItem;
UpdateTitle.DataBindings.Add("Text", bs, "Title");
UpdateDate.DataBindings.Add("Value", bs, "UpdatedOn");
}
}
public class Project
{
//...
public BindingList<Update> AvailableUpdates { get; set; }
}
//...
private void ReadUpdates()
{
AvailableUpdates = new BindingList<Update>();
for (int i = 0; i < 10; i++)
{
AvailableUpdates.Add(new
Update(i, DateTime.Now.AddDays(i)));
}
}
你会在这里找到一个非常相似的问题的更详细的答案:
C# - 组合框除非您先更改选择,否则不会更新其显示列表 - Stack Overflow [ ^ ]
这篇关于窗口中的小复杂数据绑定形成组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文