WPF ListBox's itemsource depend on another listbox's selecteditem

//xaml
<ListBox Grid.Row="1" Grid.Column="0" ItemsSource="{Binding}" 
         x:Name="countryLbx" 
         DisplayMemberPath="CountryName"/> 
<ListBox Grid.Row="1" Grid.Column="1" 
         ItemsSource="{Binding Path=SelectedItem.StateList,ElementName=countryLbx}"
         x:Name="stateLbx" 
         DisplayMemberPath="StateName"/> 
<ListBox Grid.Row="1" Grid.Column="2" 
         ItemsSource="{Binding Path=SelectedItem.CityList,ElementName=stateLbx}"
         DisplayMemberPath="CityName" />

 

 

Whole code

//xaml
<Window x:Class="WpfApp215.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp215"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="ListBox">
            <Setter Property="IsSynchronizedWithCurrentItem" Value="True"/>
        </Style>
    </Window.Resources>
    <Grid ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="Country" Grid.Row="0" Grid.Column="0"/>
        <ListBox Grid.Row="1" Grid.Column="0" ItemsSource="{Binding}" 
                 x:Name="countryLbx" 
                 DisplayMemberPath="CountryName"/>
        <TextBlock Text="State" Grid.Row="0" Grid.Column="1"/>
        <ListBox Grid.Row="1" Grid.Column="1" 
                 ItemsSource="{Binding Path=SelectedItem.StateList,ElementName=countryLbx}"
                 x:Name="stateLbx" 
                 DisplayMemberPath="StateName"/>
        <TextBlock Text="City" Grid.Row="0" Grid.Column="2"/>
        <ListBox Grid.Row="1" Grid.Column="2" 
                 ItemsSource="{Binding Path=SelectedItem.CityList,ElementName=stateLbx}"
                 DisplayMemberPath="CityName" />
    </Grid>
</Window>




//cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json;

namespace WpfApp215
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var countryList = new List<Country>()
            {
                new Country
                {
                    CountryId=1,
                    CountryName="USA",
                    StateList=new List<State>()
                    {
                        new State
                        {
                            StateId=1,
                            StateName="NY",
                            CityList=new List<City>()
                            {
                                new City
                                {
                                    CityId=1,
                                    CityName="NY"
                                },
                                new City
                                {
                                    CityId=2,
                                    CityName="UpTown"
                                },
                                new City
                                {
                                    CityId=3,
                                    CityName="MiddleTown"
                                },
                                new City
                                {
                                    CityId=4,
                                    CityName="DownTown"
                                }
                            }
                        },
                        new State
                        {
                            StateId=2,
                            StateName="CA",
                            CityList=new List<City>()
                            {
                                new City
                                {
                                    CityId=1,
                                    CityName="SF"
                                },
                                new City
                                {
                                    CityId=2,
                                    CityName="LS"
                                },
                                new City
                                {
                                    CityId=3,
                                    CityName="SD"
                                },
                                new City
                                {
                                    CityId=4,
                                    CityName="Oakland"
                                }
                            }
                        },
                        new State
                        {
                            StateId=3,
                            StateName="DX",
                            CityList=new List<City>()
                            {
                                new City
                                {
                                    CityId=1,
                                    CityName="DS"
                                },
                                new City
                                {
                                    CityId=2,
                                    CityName="Austin"
                                },
                                new City
                                {
                                    CityId=3,
                                    CityName="Waco"
                                }
                            }
                        }
                    }
                },
                new Country
                {
                    CountryId=2,
                    CountryName="USA2",
                    StateList=new List<State>()
                    {
                        new State
                        {
                            StateId=1,
                            StateName="WA",
                            CityList=new List<City>()
                            {
                                new City
                                {
                                    CityId=1,
                                    CityName="Seattle"
                                },
                                new City
                                {
                                    CityId=2,
                                    CityName="Portland"
                                },
                                new City
                                {
                                    CityId=3,
                                    CityName="Everett"
                                }
                            }
                        },
                        new State
                        {
                            StateId=2,
                            StateName="NZ",
                            CityList=new List<City>()
                            {
                                new City
                                {
                                    CityId=1,
                                    CityName="NZC"
                                },
                                new City
                                {
                                    CityId=2,
                                    CityName="Hartford"
                                }
                            }
                        },
                        new State
                        {
                            StateId=3,
                            StateName="MT",
                            CityList=new List<City>()
                            {
                                new City
                                {
                                    CityId=1,
                                    CityName="Boston"
                                },
                                new City
                                {
                                    CityId=2,
                                    CityName="Concord"
                                }
                            }
                        }
                    }
                },
                new Country
                {
                    CountryId=3,
                    CountryName="USA3",
                    StateList=new List<State>()
                    {
                        new State
                        {
                            StateId=1,
                            StateName="FL",
                            CityList=new List<City>()
                            {
                               new City
                               {
                                   CityId=1,
                                   CityName="MA"
                               },
                                new City
                                {
                                    CityId=2,
                                    CityName="Orlando"
                                }
                            }
                        }
                    }
                }
            };
            this.DataContext = countryList;
        }
    }

    public class Country
    {
        public int CountryId { get; set; }
        public string CountryName { get; set; }
        public List<State> StateList { get; set; }
    }

    public class State
    {
        public int StateId { get; set; }
        public string StateName { get; set; }
        public List<City> CityList { get; set; }
    }

    public class City
    {
        public int CityId { get; set; }
        public string CityName { get; set; }
    }
}

 

 

posted @ 2024-07-16 11:25  FredGrit  阅读(5)  评论(0编辑  收藏  举报