简体   繁体   English

使用ItemsSource时,操作无效。 使用ItemsControl.ItemsSource访问和修改元素

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

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. 我正在尝试制作2个列表框,我可以按一个按钮将从左侧列表框中选择的项目添加到右侧列表框中。 Here's the XAML for the listboxes: 这是列表框的XAML:

        <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#: 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;
            }
        }
    }

I get the exception on: 我得到例外:

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: 错误是:

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

Any suggestions? 有什么建议么?

You can't modify ListBox's Items when the items populated through ItemsSource . 当通过ItemsSource填充Items时,您无法修改ListBox的Items In that case you suppose to modify items in the ItemsSource collection instead. 在这种情况下,您可以改为修改ItemsSource集合中的项目。

I'd suggest to change your List to ObservableCollection . 我建议将List更改为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具有内置机制,可以在从集合中添加或删除项目时通知UI刷新:

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);
        }
    }
}

I fixed the issue by doing this: 我通过这样做解决了这个问题:

    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);

            //Delete the item from the left side list
            //ListLps.Items.RemoveAt(SelectedIndex);

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

            }
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 ItemsSource 时操作无效。 改为使用 ItemsControl.ItemsSource 访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead 使用ItemsSource时,操作无效。 在执行两次时,使用ItemsControl.ItemsSource访问和修改元素 - Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead when execution twice WPF数据网格:在使用ItemsSource时,操作无效。 - WPF Data Grid : Operation is not valid while ItemsSource is in use. 使用ItemsSource时操作无效。 但是我没有混 - Operation is not valid while ItemsSource is in use. But i'm not mixing 显示ItemsControl.ItemsSource是否为null - Show if ItemsControl.ItemsSource is null ItemsControl.ItemsSource,绑定无效 - ItemsControl.ItemsSource, binding doesn't work 根据另一个 ItemsControl 的选定对象更新 ItemsControl.ItemsSource - Update ItemsControl.ItemsSource based on selected object of a another ItemsControl 如何将ItemsControl.ItemsSource与XAML中的属性绑定? - How can I bind an ItemsControl.ItemsSource with a property in XAML? 访问Itemscontrol的ItemsSource中的对象 - Access objects in Itemscontrol's ItemsSource 无法使用列表框DataTemplate:当ItemsSource正在使用时,操作无效 - Can't use list box DataTemplate: Operation is not valid while ItemsSource is in use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM