WPF MVVM模式下ComboBox级联效果 选择第一项

MVVM模式下做的省市区的级联效果。通过改变ComboBox执行命令改变市,区。

解决主要问题就是默认选中第一项

1.首先要定义一个属性,继承自INotifyPropertyChanged接口。我这里用的Prism框架中集成的NotificationObject

        /// <summary>

        ////// </summary>

        private ObservableCollection<MyArea> provinceBindingList;

        public ObservableCollection<MyArea> ProvinceBindingList

        {

            get { return provinceBindingList; }

            set { provinceBindingList = value; this.RaisePropertyChanged("ProvinceBindingList"); }

        }

        /// <summary>

        ////// </summary>

        private ObservableCollection<MyArea> cityBindingList;

        public ObservableCollection<MyArea> CityBindingList

        {

            get { return cityBindingList; }

            set { cityBindingList = value; this.RaisePropertyChanged("CityBindingList"); }

        }

        /// <summary>

        ////// </summary>

        private ObservableCollection<MyArea> areaBindingList;

        public ObservableCollection<MyArea> AreaBindingList

        {

            get { return areaBindingList; }

            set { areaBindingList = value; this.RaisePropertyChanged("AreaBindingList"); }

        }



        /// <summary>

        /// 默认选择请选择项

        /// </summary>

        public readonly MyArea defaultSelectItem;

        /// <summary>

        /// 添加城市选择项

        /// </summary>

        private MyArea selectCity;

        public MyArea SelectCity

        {

            get { return selectCity; }

            set { selectCity = value; this.RaisePropertyChanged("SelectCity"); }

        }
属性定义

2.XAML部分

<ComboBox SelectedIndex="0" ItemsSource="{Binding ProvinceBindingList,Mode=TwoWay}"

                          SelectedItem="{Binding defaultSelectItem,Mode=TwoWay}" DisplayMemberPath="Name"

                          Margin="5,105,5,5" Width="100" Name="cboProvince" Style="{StaticResource ComboBoxStyle}">

                    <i:Interaction.Triggers>

                        <i:EventTrigger EventName="SelectionChanged">

                            <i:InvokeCommandAction Command="{Binding Path=DataContext.GetCity,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}" 

                                                                   CommandParameter="{Binding Path=SelectedValue,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}" />

                        </i:EventTrigger>

                    </i:Interaction.Triggers>

                </ComboBox>

                <ComboBox SelectedIndex="0" ItemsSource="{Binding CityBindingList,Mode=TwoWay}" DisplayMemberPath="Name"

                           SelectedItem="{Binding SelectCity,Mode=TwoWay}" Margin="5" Width="100" Grid.Row="1" 

                          Name="cboCity" Style="{StaticResource ComboBoxStyle}">

                    <i:Interaction.Triggers>

                        <i:EventTrigger EventName="SelectionChanged">

                            <i:InvokeCommandAction Command="{Binding Path=DataContext.GetArea,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}" />

                        </i:EventTrigger>

                    </i:Interaction.Triggers>

                </ComboBox>

                <ComboBox SelectedIndex="0" ItemsSource="{Binding AreaBindingList,Mode=TwoWay}" DisplayMemberPath="Name" 

                          SelectedItem="{Binding SelectCity,Mode=TwoWay}" Margin="5" Width="100" Grid.Row="2" 

                          Name="cboArea" Style="{StaticResource ComboBoxStyle}">

                    <i:Interaction.Triggers>

                        <i:EventTrigger EventName="SelectionChanged">

                            <i:InvokeCommandAction Command="{Binding Path=DataContext.GetAddCityInfoExecute,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}" 

                                                                   CommandParameter="{Binding Path=SelectedValue,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ComboBox}}" />

                        </i:EventTrigger>

                    </i:Interaction.Triggers>

                </ComboBox>
View Code

3.命令部分

//初始化命令

this.GetProvince = new DelegateCommand(GetProvinceExecute);

            this.GetCity = new DelegateCommand<MyArea>(GetCityExecute);

            this.GetArea = new DelegateCommand(GetAreaExecute);
        /// <summary>

        /// 获取省

        /// </summary>

        private void GetProvinceExecute()

        {

            var province = AreaManager.GetProvince();

            province.Insert(0, defaultSelectItem);

            ProvinceBindingList = new ObservableCollection<MyArea>(province);

        }



        /// <summary>

        /// 获取市

        /// </summary>

        /// <param name="obj"></param>

        private void GetAreaExecute()

        {

            if (SelectCity != null && SelectCity.ID != "0")

            {

                var area = AreaManager.GetAreaByCID(SelectCity);

                area.Insert(0, defaultSelectItem);

                AreaBindingList = new ObservableCollection<MyArea>(area);

            }

            else if (SelectCity == null || SelectCity != null && SelectCity.ID == "0")

                AreaBindingList = new ObservableCollection<MyArea>(new List<MyArea>() { defaultSelectItem });

        }



        /// <summary>

        /// 获取区

        /// </summary>

        /// <param name="obj"></param>

        private void GetCityExecute(MyArea province)

        {

            if (province != null && province.ID != "0")

            {

                var city = AreaManager.GetCityByPID(province);

                city.Insert(0, defaultSelectItem);

                CityBindingList = new ObservableCollection<MyArea>(city);

                SelectCity = defaultSelectItem;

            }

            else if (province == null || province != null && province.ID == "0")

                CityBindingList = new ObservableCollection<MyArea>(new List<MyArea>() { defaultSelectItem });

        }
命令对应方法

 

你可能感兴趣的:(combobox)