首页 > 其他分享 >WPF ListBox's itemsource depend on another listbox's selecteditem

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

时间:2024-07-16 11:29:19浏览次数:9  
标签:City depend selecteditem CityName System CityId using new ListBox

//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; }
    }
}

 

 

标签:City,depend,selecteditem,CityName,System,CityId,using,new,ListBox
From: https://www.cnblogs.com/Fred1987/p/18304804

相关文章

  • flutter pub get 的时候:A dependency specification must be a string or a mapping.
    想在pubspec.yaml文件中添加字体:报错了fonts:-family:MiaoZifonts:-asset:assets/fonts/MiaoZi-YunYingTi-2.ttfweight:500看了这篇文章解决了我原来是加在dependencies:flutter:sdk:flutter#新添加的依赖fonts:......
  • Spring的依赖注入DI(dependency injection)的两种方式
    前面的是没有依赖时如何创建对象,现在是有依赖时如何创建对象。IOC的作用:降低程序间的依赖关系。但是不是消除依赖关系,所以程序间必然有一些依赖关系,依赖关系的管理以后都交给spring来维护,什么是依赖关系呢?就是在当前类中需要用到其他类的对象,由spring为我们提供,我们只需要在配置......
  • maven </dependencies>和</dependencyManagement> 有什么区别
    在Maven的pom.xml文件中,和元素有不同的用途和作用域::这个元素用来列出项目直接依赖的库和插件。每个元素定义了一个依赖项,包括groupId、artifactId、version等信息。当Maven构建项目时,它会解析中列出的所有依赖项,并将其包含在项目的构建过程中。元素位于pom.......
  • Derect local .aar file dependencies are not supported when building an AAR
    背景项目中需要将部分功能业务打包成aar包供其它项目调用,我引入了fataar(具体使用方式见github)。实际打包过程中抛出错误Derectlocal.aarfiledependenciesarenotsupportedwhenbuildinganAAR原因分析我们项目中lib文件夹下导入了很多jar包、aar包,在之前没有引入aar包......
  • flutter项目报错[!] The ‘Pods-Runner‘ target has transitive dependencies that i
    运行flutter项目报错[!]The'Pods-Runner'targethastransitivedependenciesthatincludestaticallylinkedbinaries:(AMap2DMap/MAMapKit.framework,AMapLocation/AMapLocationKit.framework,andPods/AMapSearch/AMapSearchKit.framework)解决方案:使用静态框架......
  • Error creating bean with name 'userServiceImpl': Unsatisfied dependency expresse
     原因是:Property'sqlSessionFactory'or'sqlSessionTemplate'arerequired,检查一下这两个类是干什么的:SqlSessionFactory是MyBatis的重要对象之一,是创建SqlSession的工厂。SqlSessionTemplate是MyBatis-Spring的核心,是MyBatis为了接入Spring提供的Bean,这个......
  • 【winform】ListBox如何给item项添加hover
    1、绑定move事件listBox1.MouseMove+=newMouseEventHandler(listBox_MouseMove);2、编写代码privatevoidlistBox_MouseMove(object?sender,MouseEventArgse){ListBox?listBox=senderasListBox;//获取鼠标在ListBox中的位置int......
  • MFC---列表框控件ListBox、组合框控件Combo Box(常用控件)
    前面两节讲了比较常用的按钮控件,并通过按钮控件实例说明了具体用法。本文要讲的是列表框控件(ListBox)及其使用实例。列表框控件简介列表框给出了一个选项清单,允许用户从中进行单项或多项选择,被选中的项会高亮显示。列表框可分为单选列表框和多选列表框,顾名思义,单选列表框中......
  • springboot dependencyManagement的作用
    一 dependencyManagement的用处在创建springboot多模块的项目中,为了保持各模块的相同依赖保持一致,通常会在项目级的POM.XML中使用 dependencyManagement节点来实现这个一致性。项目级pom.xml<!--依赖声明--><dependencyManagement><dependencies>......
  • dependencies与dependencyManagement
    dependencyManagement统一多模块的依赖版本如果你的项目有多个子模块,而且每个模块都需要引入依赖,但为了项目的正确运行,必须让所有的子项目(以下子项目即指子模块)使用依赖项的统一版本,才能保证测试的和发布的是相同的结果。Maven使用dependencyManagement来统一模块见的依赖......