0
votes

I have an ObservbleCollection with binding to listbox. After I put the collection as ItemsSource of the list, I cannot update my collection. If I update it, the program close (without any crash).

The Code:
I have the class:

class MyFile
{
    String FileName {get; set;}
    ImageSource Ico {get; set;}
}

Then run the code in the constractor (after InitializeComponents)

ObservableCollection<MyFile> filesList = new ObservableCollection<MyFile>();
filesList.Add(new MyFile { Name = "bar.doc", Ico = null } // Work Fine
filesList.Add(new MyFile { Name = "foo.txt", Ico = null } // Work Fine
files.ItemsSource = filesList; 
filesList.Add(new MyFile { Name = "try.txt", Ico = null } // EXIT FROM PROGRAM

What's wrong in my program?

Edit
Just tested it with null instead GetIcon

2
Can you show the GetIcon method? - Hossein Narimani Rad
It very complicated, I got it from the internet. If I will put null instead the program act the same - nrofis
Thats weird, What is the type of files? - Shani Elharrar
Just ListBox with templates - nrofis
If binding is on the XAML what does "files.ItemsSource = filesList;" stand there for? I repeat again read the docs and see the samples: to bind objects you should use DependencyPropertyes or implement INotyfyPropertyChangend. But, once more, read the docs!!!! At least the chapter regarding data binding, you're falling on the really basic stuff. - Alessandro Rossi

2 Answers

0
votes

The ObservableCollection's mission is to announce changes (add, remove, ...) to the listener. So nothing is wrong with ObservableCollection and binding it as the ItemsSource of the ListBox, will not make it uneditable. It must be something wrong with the GetIcon method (e.g. trying to reopen the same image which was not close properly)

To see if this idea is correct try this:

ObservableCollection<MyFile> filesList = new ObservableCollection<MyFile>();
filesList.Add(new MyFile { Name = "bar.doc", Ico = GetIcon(".doc") }
//filesList.Add(new MyFile { Name = "foo.txt", Ico = GetIcon(".txt") } // Comment this line
files.ItemsSource = filesList; 
filesList.Add(new MyFile { Name = "try.txt", Ico = GetIcon(".txt") } 

If you want to see the bug. I suggest you copy those lines in a click event of a button (and not in the constructor) then run your application.

0
votes

Here is a small sample on binding an ObservableCollection using DependencyPropery. I wish it helps you on how to do DataBinding in this way.

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        x:Name="MyWindow"

        Title="MainWindow">
    <Grid>
        <ListBox ItemsSource="{Binding MyList, ElementName=MyWindow, Mode=TwoWay}">
        </ListBox>
    </Grid>
</Window>

Code Behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Threading;

using System.Collections.ObjectModel;

namespace WpfApplication1
{
    /// <summary>
    /// Logica di interazione per MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static readonly DependencyProperty MyListProperty = DependencyProperty.Register("MyList", typeof(ObservableCollection<string>), typeof(MainWindow));

        public ObservableCollection<string> MyList
        {
            get
            {
                return (ObservableCollection<string>)GetValue(MyListProperty);
            }
            set
            {
                SetValue(MyListProperty, value);
            }
        }
        public MainWindow()
        {
            InitializeComponent();

            MyList = new ObservableCollection<string>() { "a", "b" };
            MyList.Add("c");
        }
    }
}