简体   繁体   中英

Remove data from a bound list C#

i am currently trying to remove items from a bound list.

Here is where it is bound in the xaml.

<ListBox Height="362" HorizontalAlignment="Left" Margin="6,245,0,0" Name="lstHoldCategories" VerticalAlignment="Top" Width="462" SelectionChanged="list_SelectionChanged_1" BorderThickness="0,0,0,0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <!--This positions the Text eg name etc-->
            <StackPanel Orientation ="Vertical">
                <!--This changes the size of the photo on the left-->
                <Image Width="445" Height="300" HorizontalAlignment="Center" Stretch="UniformToFill" >
                    <Image.Source>
                        <BitmapImage UriSource="{Binding imgSource}"/>
                    </Image.Source>
                </Image>
                <TextBlock Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}" Width="1000"  />
                <TextBlock Text="{Binding Type}" Style="{StaticResource PhoneTextLargeStyle}" Width="1000"  />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I have then made a seperate generic list to be held in a seperate unbound listBox so that i can select a "Type" and load up all the animals of that type.

Here is the code where i set up the unbound list

public CategorySearch()
    {
        InitializeComponent();
        observablePets = new ObservableCollection<Shop>();
        temp = new ObservableCollection<Shop>();

        MyList.Add("Dog");
        MyList.Add("Cat");
        MyList.Add("Fish");
        MyList.Add("Lizard");

        lstCategory.ItemsSource = MyList;
    }

and this is where i have done the SelectedIndex of the unbound listBox to add in the animals of the selected "Type"

private void lstCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (lstCategory.SelectedIndex == 0)
        {
            foreach (Shop pet in thisApp.myshop)
            {
                if (pet.Type == "Dog")
                {
                    //lstHoldCategories.Items.Clear();
                    temp.Add(pet);                       
                    lstHoldCategories.ItemsSource = temp;
                }
            }
        }
        if (lstCategory.SelectedIndex == 1)
        {
            foreach (Shop pet in thisApp.myshop)
            {
                if (pet.Type == "Cat")
                {
                    //lstHoldCategories.Items.Clear();
                    temp.Add(pet);
                    lstHoldCategories.ItemsSource = temp;
                }
            }
        }
        if (lstCategory.SelectedIndex == 2)
        {
            foreach (Shop pet in thisApp.myshop)
            {
                if (pet.Type == "Fish")
                {
                    //lstHoldCategories.Items.Clear();
                    temp.Add(pet);
                    lstHoldCategories.ItemsSource = temp;
                }
            }
        }
        if (lstCategory.SelectedIndex == 3)
        {
            foreach (Shop pet in thisApp.myshop)
            {
                if (pet.Type == "Lizard")
                {
                    //lstHoldCategories.Items.Clear();
                    temp.Add(pet);
                    lstHoldCategories.ItemsSource = temp;
                }
            }
        }
    }

As you can see in this piece of code, I have commented out the piece of code that i believed would empty the listBox on the selectedIndex and reload the listBox with the new selection. Unfortunately it doesn't work and crashes the app when you select an index.

If there is a different way to empty the listBox that is bound, i would appreciate someone advising me how to do it, Thanks in advance, Jason

////Pics\\\\ This is what the page looks like before an index is selected

在此处输入图片说明

This is what the bound listBox will look like when you select an index

在此处输入图片说明

You need to clear the collection itself, rather than the object bound to the collection. A quick search showed up this... Delete all items from listobox
Just to clarify, the Items collection lives on the ListBox and that property is readonly. So you need to remove the items from the collection your ListBox is actually bound to.
you should just be able to call clear on temp prior to adding you new items. But you will need to make sure your collection source implements the INotifyCollectionChanged to see the changes reflected in the UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM