Remove item - listbox

0

witam.mam listę do której mogę dodać zdjęcie ale nie mogę z niej usunąć zaznaczonego zdjęcia bo wywala komunikat:

Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
Ktoś zna przyczynę?

<Window x:Class="remove.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <DataTemplate x:Key="zawartosc">
            <Border BorderThickness="1" BorderBrush="Black">
                <Image Source="{Binding UriSource.LocalPath}" Width="125"
                   Height="125" Margin="3" />
            </Border>
        </DataTemplate>
    </Window.Resources>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        
        <StackPanel Grid.Row="0" Orientation="Vertical">
            <Button Click="Dodaj_zdjecie" Content="Dodaj" Height="23" Width="auto" ></Button>
            <Button  Click="usun_Click" Content="Usun" Height="23" Name="usun" Width="auto" />
        </StackPanel>           

            <ScrollViewer Grid.Row="1" Name="scrollViewer"  VerticalScrollBarVisibility="Visible" >
                <ListBox  Name="lista" HorizontalAlignment="Stretch" Margin="5" VerticalAlignment="Stretch"
                 ItemsSource="{Binding}" ItemTemplate="{StaticResource zawartosc}">
                </ListBox>
            </ScrollViewer>
    </Grid>       
</Window>

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Microsoft.Win32;

namespace remove
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<BitmapSource> kolekcja = new ObservableCollection<BitmapSource>();

        public MainWindow()
        {
            InitializeComponent();
            lista.ItemsSource = kolekcja;
        }

        string wybrany_plik;

        private void Dodaj_zdjecie(object sender, RoutedEventArgs e)
        {
            OpenFileDialog plik = new OpenFileDialog();
            plik.Title = "Wybierz zdjęcie";
            plik.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|"
                + "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|"
                + "Portable Network Graphic (*.png)|*.png";

            if (plik.ShowDialog() == true)
            {
                wybrany_plik = plik.FileName;
                kolekcja.Add(new BitmapImage(new Uri(wybrany_plik.ToString())));
            }
        }

        private void usun_Click(object sender, RoutedEventArgs e)
        {
            lista.Items.RemoveAt(lista.SelectedIndex);
        }
    }
}
0

Spróbuj coś takiego:

listBox.Items.RemoveAt(listBox.Items.IndexOf(listBox.SelectedItem));
0

Już sobie poradziłem z problemem:

kolekcja.Remove((BitmapSource)lista.SelectedItem); 

1 użytkowników online, w tym zalogowanych: 0, gości: 1