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

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

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

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

        /// 
        ////// 
        private ObservableCollection provinceBindingList;
        public ObservableCollection ProvinceBindingList
        {
            get { return provinceBindingList; }
            set { provinceBindingList = value; this.RaisePropertyChanged("ProvinceBindingList"); }
        }
        /// 
        ////// 
        private ObservableCollection cityBindingList;
        public ObservableCollection CityBindingList
        {
            get { return cityBindingList; }
            set { cityBindingList = value; this.RaisePropertyChanged("CityBindingList"); }
        }
        /// 
        ////// 
        private ObservableCollection areaBindingList;
        public ObservableCollection AreaBindingList
        {
            get { return areaBindingList; }
            set { areaBindingList = value; this.RaisePropertyChanged("AreaBindingList"); }
        }

        /// 
        /// 默认选择请选择项
        /// 
        public readonly MyArea defaultSelectItem;
        /// 
        /// 添加城市选择项
        /// 
        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(GetCityExecute);
            this.GetArea = new DelegateCommand(GetAreaExecute);
        /// 
        /// 获取省
        /// 
        private void GetProvinceExecute()
        {
            var province = AreaManager.GetProvince();
            province.Insert(0, defaultSelectItem);
            ProvinceBindingList = new ObservableCollection(province);
        }

        /// 
        /// 获取市
        /// 
        /// 
        private void GetAreaExecute()
        {
            if (SelectCity != null && SelectCity.ID != "0")
            {
                var area = AreaManager.GetAreaByCID(SelectCity);
                area.Insert(0, defaultSelectItem);
                AreaBindingList = new ObservableCollection(area);
            }
            else if (SelectCity == null || SelectCity != null && SelectCity.ID == "0")
                AreaBindingList = new ObservableCollection(new List() { defaultSelectItem });
        }

        /// 
        /// 获取区
        /// 
        /// 
        private void GetCityExecute(MyArea province)
        {
            if (province != null && province.ID != "0")
            {
                var city = AreaManager.GetCityByPID(province);
                city.Insert(0, defaultSelectItem);
                CityBindingList = new ObservableCollection(city);
                SelectCity = defaultSelectItem;
            }
            else if (province == null || province != null && province.ID == "0")
                CityBindingList = new ObservableCollection(new List() { defaultSelectItem });
        }
命令对应方法

 

转载于:https://www.cnblogs.com/xcong/p/3467754.html

你可能感兴趣的:(c#)