Question

I have this code which works fine

public partial class MainWindow : Window
{
    private static ObservableCollection<Archive> _archiveList = new ObservableCollection<Archive>();
    public static ObservableCollection<Archive> archiveList { get { return _archiveList; } }
}
private void build_archiveList()
{
    // create new Archive
    // add the new Archive to archiveList
}

...but if I try to use auto implemented properties it just won't work and I don't understand why. Code with auto implemented properties:

public partial class MainWindow : Window
{
    public static ObservableCollection<Archive> archiveList { get; private set; }
    public MainWindow()
    {
            InitializeComponent();

            archiveList = new ObservableCollection<Archive>();
            build_archiveList();
    }
}

Why doesn't the second approach work?

EDIT: sorry for being incomplete, the list has binding with a datagrid and the datagrid remains empty when using the second approach (altough the new archive is added since archiveList.count is increased with both approaches)

how build_archiveList adds an archive to the list (observablecollection):

tmpArchive.content.Add(new ArchiveFile(bfile.FileName, bfile.Crc.ToString(), false));
archiveList.Add(tmpArchive);
Was it helpful?

Solution

Move

archiveList = new ObservableCollection<Archive>();

to the static constructor.

Explanation

You bind to auto property (binding occurs in InitializeComponent)which is null, then you add new list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top