插入时列表框项目消失/不显示

本文关键字:显示 消失 项目 列表 插入 | 更新日期: 2024-09-23 16:08:59

我正在两个列表框之间创建一个数据传输控件。我在两个框中都显示了项目,其中文本和属性值包含数据。不过,当我从一个传输到另一个时,我可以访问传输的项(使用调试)并查看文本属性(而不是值属性),但它不会显示在我发送它的列表框中。我甚至尝试过刷新这个对象,但没有成功。

有人能告诉我我做错了什么吗?

private void btnToLeft_Click(object sender, EventArgs e)
    {
            Telerik.WinControls.UI.RadListDataItem item = new Telerik.WinControls.UI.RadListDataItem(lstRight.SelectedItem.DisplayValue.ToString(), lstRight.SelectedItem.Value);
            lstLeft.Items.Add(item);
            lstRight.Items.RemoveAt(lstRight.SelectedItem.RowIndex);
            lstLeft.Refresh();
            lstRight.Refresh();
    }

插入时列表框项目消失/不显示

这是我在类似情况下使用的代码。

private void btnToLeft_Click(object sender, EventArgs e)
{
      if (lstRight.Items.Count == 0) { return; }
      if (lstRight.SelectedItem == null) { return; }
      RadListDataItem item = lstRight.SelectedItem;
      lstRight.Items.Remove(item);
      lstLeft.Items.Add(item);
}

你可以像这样让它更通用一点。

private void MoveToTargetListBox(RadListControl sourceListBox, RadListControl targetListBox)
{
  try
  {
    if (sourceListBox.Items.Count == 0) { return; }
    if (sourceListBox.SelectedItem == null) { return; }
    RadListDataItem item = sourceListBox.SelectedItem;
    sourceListBox.Items.Remove(item);
    targetListBox.Items.Add(item);
  }
  catch (Exception ex)
  {
    //handle Exception
  }
}
private void btnToLeft_Click(object sender, EventArgs e)
{
  MoveToTargetListBox(lstRight, lstLeft);
}
private void btnToRight_Click(object sender, EventArgs e)
{
  MoveToTargetListBox(lstLeft, lstRight);
}

我想我想通了。我引用了lstRight.SelectedItem.DisplayValue.ToString(),而不是文本值-lstRight.SelectedItems.text

现在似乎起作用了!