MasterDetails按需加载问题

时间:2010-05-24 05:56:08

标签: wpf

作为学习wpf和理解绑定如何工作的练习,我有一个有效的例子。但是当我尝试按需加载时,我失败了。

我基本上有3个课程Country-City-Hotels

如果我一次性加载ALL,那么如果我按需加载它就会失败。 我做错了什么?

作品

        <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 个答案:

答案 0 :(得分:1)

刚刚意识到让一切顺利的事情! 我不见了

IsSynchronizedWithCurrentItem = “真”

现在这是有效的。