簡體   English   中英

mvvm綁定selecteditem以更新listview

[英]mvvm binding selecteditem to update listview

我是MVVM的新手,並且一直試圖將工作程序轉換為MVVM程序。 我一直在尋找答案,但到目前為止還沒有運氣。

基本上我擁有的是:列表框和列表視圖。 列表框中填充了Trainstations,我想在列表視圖中顯示站點的時間(有延遲等)。 列表框中充滿了工作站,每當我選擇一個工作站時,它都會更新,我將它放在一個名為“CurrentStation”的變量中。 現在我正在使用這個'CurrentStation'獲取一個ObservableCollection列表,列出該站的所有離開,但由於某種原因,該功能只被調用一次,並且當我選擇另一個站時不會更新。

我也不知道在xaml代碼中綁定什么。

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

這是ViewModel代碼:

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

我想你需要:

  • 明確更新Departures屬性,可以在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"); } } 
  • 觸發更改通知,該更改通知將使用着名的OnPropertyChanged刷新離開綁定(和列表框!)

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

您還必須在Observabe集合上設置OnPropertyChanged

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM