1

I have an ObservableCollection which is viewed in a custom listbox. I need the listbox to update the view according to changes applied, like inserting new feeds or removing feeds from the ObservableCollection

parts of code are available below

public class lbl
{
    public ObservableCollection<feed> ModifiedItems 
        = new ObservableCollection<feed>();

    public lbl()
    {
        InitializeComponent();
        listBox1.ItemsSource = ModifiedItems ;
    }

    public void update(object sender, EventArgs e)
    {
        var x = ModifiedItems.Last();
        listBox1.Items.Add(x);
    }
}

public class feed
{
    public int ID { get; set; }
    public int source_id { get; set; }
    public string title { get; set; }
    public string source_icon { get; set; }
    public string url { get; set; }
    public string pudate { get; set; }
}

XAML

<ListBox x:Name="listBox1" >
    <ListBox.ItemTemplate >
        <DataTemplate >
            <StackPanel Width="400" Margin="20" >
                <Button x:Name="pic"  Tag="{Binding Id}">
                    <Button.Template>
                        <ControlTemplate>
                            <TextBlock Text="{Binding title}" TextWrapping="Wrap" FontFamily="Arial" FontSize="28" Tag="{Binding Id}"/>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
                <TextBlock Text="{Binding pudate}" TextWrapping="Wrap" FontSize="24"/>
                <Image Source="{Binding source_icon}" Width="100" Height="60"/>
           </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Note: This is not part of the code. It gives me the error when trying to add an Item "Operation not supported on read-only collection."

I tried the solution posted here Implementing CollectionChanged and still I get the same error.

Any help please, Thanks in advance

Community
  • 1
  • 1
Salma Hamed
  • 2,000
  • 4
  • 26
  • 45

2 Answers2

5

The problem is with your update method:

 public void update(object sender, EventArgs e)
 {
     var x = ModifiedItems.Last();
     listBox1.Items.Add(x);
 }

The ItemsSource of your ListBox is set to the ModifiedItems which is an ObservableCollection. Therefore if you add or remove items from this collection, the ListBox UI will update automatically. For example, to add a new items to your view simply do the following:

ModifiedItems.Add(new feed());

This is the whole point of an ObservableCollection, the view can observe it!

If, rather than adding / removing items, you are updating existing items, you will need to make feed implement INotifyPropertyChanged.

ColinE
  • 68,894
  • 15
  • 164
  • 232
0

Since you're setting the ItemsSource of the ListBox, you're binding the ModifiedItems collection to it.

This means you have to modify ModifiedItems, and not the ListBox to add/remove items, which then will update accordingly.

public void update(object sender, EventArgs e)
{
    var x = ModifiedItems.Last();
    ModifiedItems.Items.Add(x);
}

Why you want to duplicate the last item is beyond me. But that's the change you need to do.

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150