【发布时间】:2012-03-28 12:20:38
【问题描述】:
我有两个列表,第一个(右)表示所有汽车的列表,第二个(左)表示待售汽车的列表。
在我的控制中有两个 ListBox,每个都将显示一个汽车列表。我想单击一个按钮 (
这两个列表是在控件类之外创建的,所以我需要绑定到 ListBoxes。我曾尝试使用 DataSource,但如果我设置它,我将无法使用 Add Remove from Items。最好的方法是什么?
谢谢。
OBS:我已更改为 ListBox。
我正在使用的解决方案是:非常非常丑陋的解决方案...:/
public IList<Item> ItensToMaintaim
{
get { return (IList<Item>)this.itensToMainTainList.DataSource; }
set
{
//Need to set null to refresh
this.itensToMainTainList.DataSource = null;
this.itensToMainTainList.DataSource = value;
this.itensToMainTainList.DisplayMember = "Name";
this.itensToMainTainList.ValueMember = "Name";
}
}
public IList<Item> Itens
{
get { return (IList<Item>)this.itensList.DataSource; }
set
{
//Need to set null to refresh
this.itensList.DataSource = null;
this.itensList.DataSource = value;
this.itensList.DisplayMember = "Name";
this.itensList.ValueMember = "Name";
}
}
private void removeItem_Click(object sender, EventArgs e)
{
if (this.itensToMainTainList.SelectedItem != null)
{
this.itens2.Remove((Item)this.itensToMainTainList.SelectedItem);
this.ItensToMaintaim = this.itens2;
if (this.itensToMainTainList.SelectedIndex < 0)
{
this.itensToMainTainList.SelectedIndex = this.itens2.Count - 1;
}
}
}
private void addItem_Click(object sender, EventArgs e)
{
if (this.itensList.SelectedItem != null)
{
bool contains = false;
contains = this.itens2.Contains(this.itensList.SelectedItem);
if (!contains)
{
this.itens2.Add((Item)this.itensList.SelectedItem);
this.ItensToMaintaim = this.itens2;
}
if (this.itensList.SelectedIndex < this.itens1.Count - 1)
{
this.itensList.SelectedIndex++;
}
}
}
【问题讨论】:
-
您是否尝试设置 DataSource 并从列表本身中删除项目?
-
是的,但是一旦我设置了 DataSource,它就不再更新了,如果我更改了原始列表,组合框仍然会显示旧的,我尝试重置 DataSource,但没有任何变化。
-
您使用哪种类型的 Windows 应用程序? (Winform / SilverLight 等)。