mvvm binding selecteditem to update listview

喜你入骨 提交于 2020-01-14 05:13:08

问题


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;
            }
        }

回答1:


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");
        }
    }
    



回答2:


You have to set OnPropertyChanged on Observabe collection too

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


来源:https://stackoverflow.com/questions/19577978/mvvm-binding-selecteditem-to-update-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!