Crazy behavior in loop c# wpf

I have a really strange behavior in one of my view models.

First, in this directory I have three csv files. When I start debugging first it goes inside the foreach and then it goes to the first line this.allDatabases.Add(Path.GetFileNameWithoutExtension(item)); and some strange behavior happens in the next step, it goes to the view. I need to get the name of the files and add them in a collection. In another class this method is working good, but now it is just ... I don't know.

For more information, I used Visual Studio 2015 Community and .NET Framework 4.6

I don't have any exceptions. The FileConstants.PATH_TO_DATABASE is a directory, and I have three csv files on it. For a better explanation I will show two pictures.

First

enter image description here

and next step of debug is

enter image description here

And my code:

private ObservableCollection<string> allDatabases;

public IEnumerable<string> AllDatabases
    {
        get
        {
            foreach (var file in Directory.GetFiles(DirectoryPath, "*.csv", SearchOption.AllDirectories))
            {
                this.allDatabases.Add(Path.GetFileNameWithoutExtension(file)); // after first add is go in view and not return back in foreach loop
            }

            return this.allDatabases;
        }
    }

And the view

<Window x:Class="Growthanalyzer.App.Views.Dialogs.NewView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Growthanalyzer.App.Views.Dialogs"
    xmlns:vms="clr-namespace:Growthanalyzer.App.ViewModels.Dialogs"
    mc:Ignorable="d"
    Title="New" Height="300" Width="300">
<StackPanel Orientation="Vertical">
    <StackPanel.DataContext>
        <vms:NewViewModel />
    </StackPanel.DataContext>

    <ListBox ItemsSource="{Binding AllDatabases}" MinHeight="150" Width="250" />
</StackPanel>

I don't really know what is happening and what is the problem. If any more information is needed just ask me.

How can I fix this bug, and from what is possible to receive this bug?

Jon Skeet
people
quotationmark

Okay, I think I see at least one potential problem (and definitely code to be avoided)...

Every time you evaluate the AllDatabases property, you add all the files to the collection. Property fetches shouldn't do that - they shouldn't change state.

Instead, you should populate the collection once (e.g. in the constructor) and then your AllDatabases property should just return a reference to the collection - quite possibly typed as ObservableCollection<string> as it's reasonable for clients to know that it is observable.

I don't know for sure whether that will fix the problem, but I can easily understand that binding a property which modifies the collection every time it's accessed could easily create some really weird behaviour.

people

See more on this question at Stackoverflow