Class loads contents in code but doesn't show on Screen in WPF

I have a class called Station*s* (emphasis on the s) which have inside 3 instances of another class called Station.

In Stations_View I have:

    <Grid VerticalAlignment="Stretch" Margin="0,0,0,0" DataContext="{Binding ViewModel_Station1}" Grid.Row="0">
        <Views:Station_View Grid.Row="0"/>
    </Grid>
    <Grid VerticalAlignment="Stretch" Margin="0,0,0,0" DataContext="{Binding ViewModel_Station2}" Grid.Row="0">
        <Views:Station_View Grid.Row="0"/>
    </Grid>
    <Grid VerticalAlignment="Stretch" Margin="0,0,0,0" DataContext="{Binding ViewModel_Station3}" Grid.Row="0">
        <Views:Station_View Grid.Row="0"/>
    </Grid>

On Stations_ViewModel I construct all the Station instances, running code that loads their contents (properties like Name, etc) of all the Station instances with:

ViewModel_Station1 = new Station_ViewModel(_host, 1);
ViewModel_Station2 = new Station_ViewModel(_host, 2);
ViewModel_Station3 = new Station_ViewModel(_host, 3);
NotifyPropertyChanged("ViewModel_Station1");
NotifyPropertyChanged("ViewModel_Station2");
NotifyPropertyChanged("ViewModel_Station3");

However, even though the code runs and the properties of the Station instances are indeed loaded, in the screen it shows as they didn't loaded (for example, the Name continues to be an empty string).

The strange thing is, if I create a window with just one Station_View class alone (not inside the Stations_View), everything is loaded and shown normally.

Every pertinent variable is declared Public.

Upvotes: 0

Views: 68

Answers (1)

I reformulated it a little. The problem persisted, but then I found a simple mistake.

My code was:

public Station_ViewModel ViewModel;

but it had to be:

public Station_ViewModel ViewModel { get; set; }

You need your data to be in a Property if you want its value to be available for data binding.

Upvotes: 1

Related Questions