我有一個列表框,帶有2個按鈕,新建和刪除。新增項目到列表框中,刪除按鈕應該從列表框中刪除項目。列表框項目與從下面的文本框中存儲用戶輸入數據的類綁定。列表框刪除
private void AddListBox()
{
lstCondition.BeginUpdate();
Condition cond = new Condition("");
cond.Name = string.Format("Condition {0}", _selection.NetConditions.Count + 1);
_selection.NetConditions.Add(cond);
lstCondition.EndUpdate();
lstCondition.SelectedItem = cond;
cboNetCondition.Properties.Items.Clear();
cboNetCondition.Properties.Items.AddRange(NetCondition);
cboControlType.Properties.Items.Clear();
cboControlType.Properties.Items.AddRange(ControlType);
cboFlowRate.Properties.Items.Clear();
cboFlowRate.Properties.Items.AddRange(FlowRate);
}
private void btnNew_Click(object sender, EventArgs e)
{
AddListBox();
}
cbo項目是組合框,其數據在條件類中綁定到列表框的每個實例。
public frmNetConditions(Condition condo, Selection selection)
{
InitializeComponent();
_selection = selection;
lstCondition.DataSource = _selection.NetConditions;
condition = _selection.NetConditions.Count;
}
private void btnDelete_Click(object sender, EventArgs e)
{
selectedCondition = (Condition)lstCondition.SelectedItem;
cboControlType.SelectedIndex = -1;
cboNetCondition.SelectedIndex = -1;
cboFlowRate.SelectedIndex = -1;
txtFlowRate.Text = string.Empty;
txtStatPressure.Text = string.Empty;
txtDampOpening.Text = string.Empty;
txtDensity.Text = string.Empty;
cboDensity.SelectedIndex = -1;
lstCondition.Items.Remove(lstCondition.SelectedItem);
lstCondition.Refresh();
}
按下這個刪除按鈕後,列表框仍然包含我想刪除的項目,我不確定這是爲什麼?
更新與數據源
public List<Condition> NetConditions { get { return _netconditions; } }
你可以發佈與你的列表框相關的整個代碼嗎? – Alexei
你是否將你的列表綁定到列表或類似的東西? –
Alexei
'lstCondition'似乎不會填充,但您嘗試分配其SelectedItem。 – Alexei