2010-05-24 36 views
0

作爲學習wpf並瞭解綁定是如何工作的練習,我有一個可行的示例。但是,當我嘗試按需加載時,我慘敗了。MasterDetails按需加載問題

我基本上有3類國家,城市,酒店

如果我加載於一身去所有的工作,如果我按需加載它悲慘的失敗了。 我在做什麼錯?

作品

 <Window x:Class="MasterDetailCollectionViewSource.CountryCityHotelWindow" 
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       Title="CountryCityHotelWindow" Height="300" Width="450"> 
      <Window.Resources> 
       <CollectionViewSource Source="{Binding}" x:Key="cvsCountryList"/> 
       <CollectionViewSource Source="{Binding Source={StaticResource cvsCountryList},Path=Cities}" x:Key="cvsCityList"/> 
       <CollectionViewSource Source="{Binding Source={StaticResource cvsCityList},Path=Hotels}" x:Key="cvsHotelList"/> 
      </Window.Resources> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition/> 
        <ColumnDefinition/> 
        <ColumnDefinition/> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto"/> 
        <RowDefinition/> 
       </Grid.RowDefinitions> 
       <TextBlock Grid.Column="0" Grid.Row="0" Text="Countries"/> 
       <TextBlock Grid.Column="1" Grid.Row="0" Text="Cities"/> 
       <TextBlock Grid.Column="2" Grid.Row="0" Text="Hotels"/> 

       <ListBox Grid.Column="0" Grid.Row="1" Name="lstCountries" ItemsSource="{Binding Source={StaticResource cvsCountryList}}" DisplayMemberPath="Name" SelectionChanged="OnSelectionChanged"/> 
       <ListBox Grid.Column="1" Grid.Row="1" Name="lstCities" ItemsSource="{Binding Source={StaticResource cvsCityList}}" DisplayMemberPath="Name" SelectionChanged="OnSelectionChanged"/> 
       <ListBox Grid.Column="2" Grid.Row="1" Name="lstHotels" ItemsSource="{Binding Source={StaticResource cvsHotelList}}" DisplayMemberPath="Name" SelectionChanged="OnSelectionChanged"/> 
      </Grid> 
     </Window> 

不起作用

XAML是上面一樣,但是我已經補充說,獲取需求的東西以下。 它只加載國家,而不是一次加載所有內容,而不需要代碼。

public CountryCityHotelWindow() 
    { 
     InitializeComponent(); 

     //Load only country Initially 
     lstCountries.ItemsSource=Repository.GetCountries(); 
     DataContext = lstCountries; 
    } 

    private void OnSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var lstBox = (ListBox)e.OriginalSource; 
     switch (lstBox.Name) 
     { 
      case "lstCountries": 
       var country = lstBox.SelectedItem as Country; 
       if (country == null) return; 
       lstCities.ItemsSource = Repository.GetCities(country.Name); 
       break; 
      case "lstCities": 
       var city = lstBox.SelectedItem as City; 
       if (city == null) return; 
       lstHotels.ItemsSource = Repository.GetHotels(city.Name); 
       break; 
      case "lstHotels": 
       break; 
     } 
    } 

我在做什麼錯了? 謝謝

回答

1

剛剛意識到一切,使一切工作! 我缺少

IsSynchronizedWithCurrentItem = 「真」

現在,這可以作爲espected。