Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ItemsSource正在使用时,操作无效。使用ItemsControl.ItemsSource访问和修改元素_C#_Wpf_Listbox - Fatal编程技术网

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

C# ItemsSource正在使用时,操作无效。使用ItemsControl.ItemsSource访问和修改元素,c#,wpf,listbox,C#,Wpf,Listbox,我正在尝试创建两个列表框,在其中我可以按一个按钮将从左侧列表框中选择的项目添加到右侧列表框中。以下是列表框的XAML: <ListBox x:Name="LeftList" Foreground="{StaticResource Foreground}" HorizontalAlignment="Left" Height="237" Margin="15,103,0,0" VerticalA

我正在尝试创建两个列表框,在其中我可以按一个按钮将从左侧列表框中选择的项目添加到右侧列表框中。以下是列表框的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"/>
当我尝试添加第二项时会发生这种情况,第一项被添加,但当您尝试添加另一项时会发生异常。错误是:

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


有什么建议吗?

我这样做解决了这个问题:

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

            }
        }

    }

通过
ItemsSource
填充项目时,无法修改列表框的
项目。在这种情况下,您应该改为修改
ItemsSource
集合中的项

我建议将您的
列表
更改为
可观察收集
。这样,从集合中删除项就足够了,因为
observateCollection
具有内置机制,在从集合中添加或删除项时通知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);
        }
    }
}
ObservableCollection leftSideList=新的ObservableCollection();
ObservableCollection rightSideList=新的ObservableCollection();
公共选择器窗口()
{
初始化组件();
添加(“360T”);
添加(“BARX”);
左侧列表。添加(“BNP”);
添加(“BOA”);
leftSideList.Add(“花旗”);
添加(“CS”);
添加(“DB”);
添加(“GS”);
添加(“JPM”);
leftSideList.Add(“RBS”);
leftSideList.Add(“瑞银”);
LeftList.ItemsSource=leftSideList;
}
私有void AddBtn_单击(对象发送方,路由目标)
{
如果(LeftList.SelectedIndex>-1)
{
int SelectedIndex=LeftList.SelectedIndex;
字符串SelectedItem=LeftList.SelectedValue.ToString();
//将所选项目添加到右侧列表
RightList.Items.Add(选择编辑项);
右侧列表。添加(选择编辑项);
if(leftSideList!=null)
{
//从ItemsSource集合中删除该项
//而不是将其从ListBox.Items中删除
leftSideList.RemoveAt(SelectedIndex);
}
}
}

理论上,当您将数据源绑定为列表的引用时,您无需清除Items集合,也无需重新绑定它。好的,我取出最后两行,它就可以工作了。我可以将多个项目添加到右侧列表中,但它不会从右侧列表中删除该项目。我使用了LeftList.Items.Remove(SelectedIndex)@kpull1I以另一种方式遇到此问题。我想我想要的措辞应该是:您正在使用绑定:修改后面的数据,而不是UI控件
    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);

            }
        }

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