【问题标题】:move items from one listbox to another listbox将项目从一个列表框移动到另一个列表框
【发布时间】:2015-07-03 09:23:28
【问题描述】:

我需要在 silverlight 应用程序中的按钮单击事件中将项目从一个列表框移动到另一个列表框。

我使用下面的代码,

private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ListBox2.Items.Add(ListBox1.SelectedItem);

            if (ListBox2.SelectedIndex != -1)
            {
                ListBox1.Items.Add(ListBox2.SelectedValue);
                ListBox2.Items.Remove(ListBox2.SelectedValue);
            }

        }

但是如果我尝试使用上面的代码,它会给出以下错误,

operation not supported on read-only collection

如何解决这个问题??

【问题讨论】:

  • @Michay No .. 我没用过
  • @Michay OP 可能没有这样做,因为 WPF 或 Silverlight 中没有 DataSource 属性。问题不在于 WinForms。

标签: c# wpf silverlight listbox silverlight-4.0


【解决方案1】:

您应该使用数据绑定将ObservableCollections 项绑定到您的两个ListBoxes 的ListBox.ItemsSource 属性:

<ListBox ItemsSource="{Binding Your1stCollectionProperty}" ... />

<ListBox ItemsSource="{Binding Your2ndCollectionProperty}" ... />

然后要移动项目,您只需调整实际集合而不是尝试调整ListBoxItems:

var itemToMove = Your1stCollectionProperty.ElementAt(indexOfItemToRemove);
Your1stCollectionProperty.Remove(itemToMove);
Your2ndCollectionProperty.Add(itemToMove);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多