记录一下ComboBox在listview中的问题,后面再解决。

在listview的ComboBox,ViewModel类得不到ComboBox的 SelectedModeIndex 和 SelectionChanged事件。

问题描述:

1. 在listview中有ComboBox

2.  数据源类 InspectionInfo ,其中有ComboBox的绑定数据源 ModelList,代码如下:

   public  class InspectionInfo : BindableBase
    {
        /// 
        /// 模式名称列表 
        /// 
        private ObservableCollection _modelList;
        public ObservableCollection ModelList
        {
            get
            {
                return _modelList;
            }
            set
            {
                SetProperty(ref _modelList, value);
            }
        }

        private ExamePartsStat _examePartInfo;
        public ExamePartsStat ExamePartInfo
        {
            get
            {
                return _examePartInfo;
            }
            set
            {
                SetProperty(ref _examePartInfo, value);
            }
        }

        private Inspection _currentInspection;
        /// 
        /// 当前的多项检查
        /// 
        public Inspection CurrentInspection
        {
            get => _currentInspection;
            set
            {
                SetProperty(ref _currentInspection, value);

            }
        }

        private int _selectedModeIndex = 0;

        public int SelectedModeIndex
        {
            get => _selectedModeIndex;
            set => SetProperty(ref _selectedModeIndex, value);
        }

        private DelegateCommand _selectionModeChangedCommand;

        /// 
        /// 模式改变事件
        /// 
        public DelegateCommand SelectionModeChangedCommand =>
            _selectionModeChangedCommand ??= new DelegateCommand(ExecuteSelectionModeChangedCommand);

        /// 
        /// 模式选择改变事件
        /// 
        /// 
        private void ExecuteSelectionModeChangedCommand(object parameter)
        {
            SharedViewModel shared = DI.Resolve();
            shared.InvokeModeChangedCommand(SelectedModeIndex);

        }
    }

3. ViewModel类,构建了listView的数据源


        private ObservableCollection _inspectionInfoList;

        public ObservableCollection InspectionInfoList
        {
            get => _inspectionInfoList;
            set => SetProperty(ref _inspectionInfoList, value);
        }

4. 页面绑定, ListView 绑定 InspectionInfoList, ComboBox的绑定数据源 ModelList,代码如下:


            
                
            
            
                
                    
                
            
        

5. 问题是 ComboBox的 SelectedModeIndex 和 SelectionChanged  事件只能在InspectionInfo类中的得到,在ViewModel类中得不到。没办法,只能通过事件,在ViewModel中得到。

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