0

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.

André Santaló
  • 639
  • 1
  • 9
  • 24
  • is `Stations_ViewModel` declared as the data context of the `Stations_View`? – Omri Btian Sep 13 '13 at 19:30
  • Yes, through the line `return new Stations_View() { DataContext = new Stations_ViewModel(_host) };` – André Santaló Sep 13 '13 at 19:48
  • I think you're looking for an `ItemsControl`. – Federico Berasategui Sep 13 '13 at 19:50
  • When does this line executes? Please post the entire code otherwise it would be rather difficult figuring out your problem ... – Omri Btian Sep 13 '13 at 19:55
  • @HighCore Yeah.. I though of using it, but since the number of Station is fixed and low (actually is 5, I simplyfied to 3 in the example), I decided not to use... but I'll try if it doesn't work this way. – André Santaló Sep 13 '13 at 19:56
  • @Omribitan It will demand some explanation of the context, but the line is reached when I debug, if this is relevant... But I'll see if I can post the code soon if it's necessary... – André Santaló Sep 13 '13 at 20:03
  • @user2777604 mayble the `NotifyPropertyChanged` is raised before you set up `Stations_ViewModel` to be the data context of `Stations_View` ... – Omri Btian Sep 13 '13 at 20:09
  • Related/dupe of: http://stackoverflow.com/questions/3989965/why-cant-i-bind-to-a-field-on-a-class-in-wpf-instead-of-binding-to-a-property or http://stackoverflow.com/questions/842575/why-does-wpf-support-binding-to-properties-of-an-object-but-not-fields – user7116 Sep 23 '13 at 19:27

1 Answers1

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.

user7116
  • 63,008
  • 17
  • 141
  • 172
André Santaló
  • 639
  • 1
  • 9
  • 24