Question

I'm new to MVVM and been trying to convert a working program to a MVVM program. I've been searching for an answer for this, but hadn't had any luck so far.

Basicly what I have is this: A listbox and a listview. The listbox is filled with Trainstations and I want the times from the station in a listview (with delay etc). The listbox is filled with stations and I whenever I select a station, it gets updated and I have it in a variable called 'CurrentStation'. Now I'm using this 'CurrentStation' to get an ObservableCollection list of all the departures from that station, but for some reason, the function is only called once and doesn't update when I select another station.

I also don't know what to bind in the xaml code.

<ListBox x:Name="lstStations" Margin="8" Grid.Row="1" ItemsSource="{Binding StationList}" SelectedItem="{Binding CurrentStation}" DisplayMemberPath="Name"/>
        <ListView Grid.Column="1" Margin="8" Grid.Row="1" ItemsSource="{Binding Departures}" >
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Time, StringFormat=t}" Header="Time" />
                    <GridViewColumn DisplayMemberBinding="{Binding Delay, Converter={StaticResource mijnDelayConverter}}" Header="Delay"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Station}" Header="Station"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Vehicle}" Header="Vehicle"/>
                    <GridViewColumn Header="Platform" CellTemplate="{DynamicResource dataTemplateTextblock}" />
<!-- Haven't had the chance to look at this ^ I don't think this is correct though -->
                    </GridView>
                </ListView.View>
            </ListView>

And here's the ViewModel code:

        public string Name
        {
            get
            {
                return "MainPage"; 
            }
        }
        public ObservableCollection<Station> StationList
        {
            get
            {
                return Station.GetStations();
            }
        }
        private Station _currentStation;
        public Station CurrentStation
        {
            get
            {
                return _currentStation;
            }
            set
            {
                _currentStation = value;
                Console.WriteLine("New station selected: " + _currentStation.ToString());
                OnPropertyChanged("CurrentStation");
            }
        }
        private ObservableCollection<Departure> _departures;
        public ObservableCollection<Departure> Departures
        {
            get
            {
                return Departure.GetDepartures(CurrentStation);
            }
            set
            {
                _departures = value;
            }
        }
Was it helpful?

Solution

I think you need to :

  • Update the Departures property explicitly, it could be done inside the CurrentStation setter

    private Station _currentStation;
    public Station CurrentStation
    {
        get
        {
            return _currentStation;
        }
        set
        {
            _currentStation = value;
            Departures = Departure.GetDepartures(_currentStation);
            Console.WriteLine("New station selected: " + _currentStation.ToString());
            OnPropertyChanged("CurrentStation");
        }
    }
    
  • Trigger the change notification that will refresh your departures binding (and listbox!) with the famous OnPropertyChanged

    private ObservableCollection<Departure> _departures;
    public ObservableCollection<Departure> Departures
    {
        get
        {
            return _departures
        }
        set
        {
            _departures = value;
            OnPropertyChanged("Departures");
        }
    }
    

OTHER TIPS

You have to set OnPropertyChanged on Observabe collection too

public ObservableCollection<Departure> Departures
    {
        get
        {
            return Departure.GetDepartures(CurrentStation);
        }
        set
        {
            _departures = value; OnPropertyChanged("Departures")
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top