使用ItemsSource时操作无效.使用ItemsControl.ItemsSource访问和修改元素 [英] Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource

查看:153
本文介绍了使用ItemsSource时操作无效.使用ItemsControl.ItemsSource访问和修改元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作2个列表框,我可以在这些列表框上按一下按钮,以将从左侧列表框选择的项目添加到右侧列表框.这是列表框的XAML:

I'm trying to make 2 list boxes where I can press on a button to add an item selected from the left listbox to the right listbox. Here's the XAML for the listboxes:

        <ListBox
        x:Name="LeftList"
        Foreground="{StaticResource Foreground}"
        HorizontalAlignment="Left" 
        Height="237" Margin="15,103,0,0" 
        VerticalAlignment="Top" 
        Width="128">
        <ListBoxItem>360T</ListBoxItem>
        <ListBoxItem>BARX</ListBoxItem>
        <ListBoxItem>BNP</ListBoxItem>
        <ListBoxItem>BOA</ListBoxItem>
        <ListBoxItem>CITI</ListBoxItem>
        <ListBoxItem>CS</ListBoxItem>
        <ListBoxItem>DB</ListBoxItem>
        <ListBoxItem>GS</ListBoxItem>
        <ListBoxItem>JPM</ListBoxItem>
        <ListBoxItem>RBS</ListBoxItem>
        <ListBoxItem>UBS</ListBoxItem>
    </ListBox>

    <ListBox 
        x:Name="RightList"
        Foreground="{StaticResource Foreground}"
        HorizontalAlignment="Left" 
        Height="237" Margin="257,103,0,0" 
        VerticalAlignment="Top" 
        Width="128"/>

C#:

List<string> leftSideList = new List<string>();
List<string> rightSideList = new List<string>();

    public ChooseLPWindow()
    {
        InitializeComponent();

        //Add to the collection leftside list
        leftSideList.Add("360T");
        leftSideList.Add("BARX");
        leftSideList.Add("BNP");
        leftSideList.Add("BOA");
        leftSideList.Add("CITI");
        leftSideList.Add("CS");
        leftSideList.Add("DB");
        leftSideList.Add("GS");
        leftSideList.Add("JPM");
        leftSideList.Add("RBS");
        leftSideList.Add("UBS");

    }

    private void AddBtn_Click(object sender, RoutedEventArgs e)
    {
        if (LeftList.SelectedIndex > -1)
        {
            int SelectedIndex = LeftList.SelectedIndex;
            string SelectedItem = LeftList.SelectedValue.ToString();

            //Add the selected item to the right side list
            RightList.Items.Add(SelectedItem);
            rightSideList.Add(SelectedItem);

            if (leftSideList != null)
            {
                //Remove the item from the collection list 
                leftSideList.RemoveAt(SelectedIndex);

                //Update the left side list
                LeftList.Items.Clear();
                LeftList.ItemsSource = leftSideList;
            }
        }
    }

我收到以下例外情况:

LeftList.Items.Clear();

当我尝试添加第二个项目时会发生这种情况,但第一个项目却被添加了,但是当您尝试添加另一个项目时发生了异常.错误是:

This happens when I try to add a second item the first one gets added but then the exception occurs when you try to add another item. The error is:

在使用ItemsSource时,该操作无效.使用ItemsControl.ItemsSource

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource

有什么建议吗?

推荐答案

通过ItemsSource填充项目时,不能修改ListBox的Items.在这种情况下,您假设改为修改ItemsSource集合中的项目.

You can't modify ListBox's Items when the items populated through ItemsSource. In that case you suppose to modify items in the ItemsSource collection instead.

我建议将您的List更改为ObservableCollection.从集合中删除项目就足够了,因为ObservableCollection具有内置的机制,可以在添加或从集合中删除项目时通知UI刷新:

I'd suggest to change your List to ObservableCollection. With that removing item from collection is enough, because ObservableCollection has built-in mechanism to notify UI to refresh whenever item added or removed from collection :

ObservableCollection<string> leftSideList = new ObservableCollection<string>();
ObservableCollection<string> rightSideList = new ObservableCollection<string>();

public ChooseLPWindow()
{
    InitializeComponent();

    leftSideList.Add("360T");
    leftSideList.Add("BARX");
    leftSideList.Add("BNP");
    leftSideList.Add("BOA");
    leftSideList.Add("CITI");
    leftSideList.Add("CS");
    leftSideList.Add("DB");
    leftSideList.Add("GS");
    leftSideList.Add("JPM");
    leftSideList.Add("RBS");
    leftSideList.Add("UBS");

    LeftList.ItemsSource = leftSideList;
}

private void AddBtn_Click(object sender, RoutedEventArgs e)
{
    if (LeftList.SelectedIndex > -1)
    {
        int SelectedIndex = LeftList.SelectedIndex;
        string SelectedItem = LeftList.SelectedValue.ToString();

        //Add the selected item to the right side list
        RightList.Items.Add(SelectedItem);
        rightSideList.Add(SelectedItem);

        if (leftSideList != null)
        {
            //Remove the item from the ItemsSource collection 
            //instead of removing it from ListBox.Items
            leftSideList.RemoveAt(SelectedIndex);
        }
    }
}

这篇关于使用ItemsSource时操作无效.使用ItemsControl.ItemsSource访问和修改元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆