MasterDetails Loading on Demand problem
Posted
by devnet247
on Stack Overflow
See other posts from Stack Overflow
or by devnet247
Published on 2010-05-24T05:56:08Z
Indexed on
2010/05/24
6:01 UTC
Read the original article
Hit count: 281
wpf
Hi
As an exercise to learn wpf and understand how binding works I have an example that works.However when I try to load on demand I fail miserably.
I basically have 3 classes Country-City-Hotels
If I load ALL in one go it all works if I load on demand it fails miserably. What Am I doing wrong?
Works
<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>
DOES NOT WORK
Xaml is the same as above, however I have added the following that fetches stuff on demand. It loads the countries only as opposed to the other one where it Loads everything at once and not code behind is necessary.
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;
}
}
What Am I doing Wrong? Thanks
© Stack Overflow or respective owner