我是 MVVM 的新手,一直在尝试将工作程序转换为 MVVM 程序。我一直在为此寻找答案,但到目前为止还没有任何运气。
基本上我所拥有的是:一个列表框和一个列表视图。列表框充满了火车站,我想要列表视图中的车站时间(有延迟等)。列表框充满了电台,每当我选择一个电台时,它都会更新,我将它放在一个名为“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;
}
}