2013-10-24 148 views
1

我是MVVM的新手,一直試圖將工作程序轉換爲MVVM程序。 我一直在尋找這個答案,但到目前爲止還沒有任何運氣。mvvm綁定selecteditem更新列表視圖

基本上我所擁有的是:列表框和列表視圖。列表框中充滿了火車站,我希望從列表視圖中的時間(延遲等)。當我選擇一個電臺時,列表框中充滿了電臺,而且我更新了電臺,並將其放入名爲「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> 

而這裏的視圖模型代碼:

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

Upvoted AirL ,但想知道你是否使用任何框架,以及如何處理你的OnPropertyChanged ... – Noctis

回答

3

我想你需要:

  • 更新Departures產權明確,它可以在CurrentStation二傳手

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

你必須設置OnPropertyChanged上Observabe收集太

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