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

时间:2014-03-31 13:24:01

标签: c# wpf listbox

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

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

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

使用ItemsSource时,操作无效。使用ItemsControl.ItemsSource

访问和修改元素

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

当通过Items填充项目时,您无法修改ListBox的ItemsSource。在这种情况下,您可以改为修改ItemsSource集合中的项目。

我建议您将List更改为ObservableCollection。使用从集合中删除项目就足够了,因为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);
        }
    }
}

答案 1 :(得分:0)

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

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

            }
        }

    }