WPF - Combobox不在列表视图中呈现(仅第一个)

时间:2015-06-22 15:08:12

标签: wpf listview mvvm combobox

我有一个WPF MVVM应用程序,我有这个问题... 我有一个列表视图,每列有几个文本框和2个组合框。 除了连击之外,一切都很好。 组合填充并包含值,仅显示选定的值 第一个组合(在列表视图的第一行)。 我尝试了一点测试并用一个文本框切换组合框并没有问题所以我猜它是组合的特定东西。 请帮我解决这个问题。

XAML:

<ListView DockPanel.Dock="Top" ItemsSource="{Binding NAPS_AgencyCodes}" SelectedItem="{Binding CurrentAgencyCode}"  MaxHeight="650" Margin="10 0" HorizontalAlignment="Left" ScrollViewer.HorizontalScrollBarVisibility="Hidden" BorderThickness="1" behave:ListBoxBehavior.ScrollOnNewItem="true">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Code" Width="80">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=Code, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"  MaxLength="4" FontSize="12" Margin="0 5 0 0" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Description" Width="300">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding Path=Description, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" FontSize="12" Margin="0 5 0 0" MaxLength="100" Width="285" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="State" Width="250">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding States}" DisplayMemberPath="StateName" SelectedValuePath="ID"  SelectedValue="{Binding Path=StateCode, Mode=TwoWay, UpdateSourceTrigger = PropertyChanged}" Margin="0 5 0 0" Width="235"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="Type Code" Width="250">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox ItemsSource="{Binding AgencyTypeCodes}" DisplayMemberPath="Description" SelectedValuePath="Code" SelectedValue="{Binding Path=AgencyTypeCode, Mode=TwoWay, UpdateSourceTrigger = PropertyChanged}" Margin="0 5 0 0" Width="235"/>                                
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

查看型号:

private NAPS_AgencyCode _agencyCode;
        private NAPS_AgencyCodeValidator _validator;
        public event Func<string> OnCodeChanged;
        public ObservableCollection<AIR_StateViewModel> States { get; set; }
        public ObservableCollection<NAPS_AgencyTypeCodeViewModel> AgencyTypeCodes { get; set; }        

        private int iCodeSerch;

        #endregion

        #region Ctor

        public NAPS_AgencyCodeViewModel(NAPS_AgencyCode agencyCode)
        {
            _agencyCode = agencyCode;
            _validator = new NAPS_AgencyCodeValidator(this);
        }

        #endregion

        #region Properties

        public string ID
        {
            get { return _agencyCode.ID;  }
            set { _agencyCode.ID = value; }
        }


        public string Code
        {
            get { return _agencyCode.Code; }
            set
            {
                if (_agencyCode.Code == value)
                    return;
                _agencyCode.Code = value;
                SetStatus();
                OnPropertyChanged("Code");              
            }
        }

        public string AgencyTypeCode
        {
            get { return _agencyCode.AgencyTypeCode; }
            set
            {
                if(_agencyCode.AgencyTypeCode == value)
                    return;
                _agencyCode.AgencyTypeCode = value;                
                SetStatus();
                OnPropertyChanged("AgencyTypeCode");                
            }
        }

        public int StateCode
        {
            get { return _agencyCode.StateCode; }
            set
            {
                if (value == _agencyCode.StateCode)
                    return;
                _agencyCode.StateCode = value;
                SetStatus();
                OnPropertyChanged("StateCode");
            }
        }

        public string Description
        {
            get { return _agencyCode.Description; }
            set
            {
                if (value == _agencyCode.Description)
                    return;
                _agencyCode.Description = value;
                SetStatus();
                OnPropertyChanged("Description");                
            }
        }

        public bool IsValid
        {
            get { return _agencyCode.IsValid; }
            set
            {
                if (value == _agencyCode.IsValid)
                    return;
                _agencyCode.IsValid = value;
                OnPropertyChanged("IsValid");
            }
        }

我有一个主视图模型

NAPS_AgencyCodes = Repository.NAPS_AgencyCodeRepository.NAPS_AgencyCodes
                            .ToObservableCollection<NAPS_AgencyCodeViewModel>(); 

非常感谢你!

1 个答案:

答案 0 :(得分:0)

O.K,我发现了我的问题...... 我用这种方式填充了组合的集合: 在约束器中,我只将2个集合填充到this.AgencyTypeCodes和this.AirStates中,而不是以这种方式复制日期:

_NAPS_AgencyCodes = (from n in _dal.GetItems()
                                select new NAPS_AgencyCodeViewModel(n)
                        {
                            ObjectStatus = Status.NoChange,
                            AgencyTypeCodes = this.AgencyTypeCodes,
                            States =this.AirStates,                            
                        }
                      ).ToObservableCollection<NAPS_AgencyCodeViewModel>();      

解决方案是给每个组合新的集合,这样就不会指向彼此的集合,如下所示:

_NAPS_AgencyCodes = (from n in _dal.GetItems()
                                select new NAPS_AgencyCodeViewModel(n)
                        {
                            ObjectStatus = Status.NoChange,
                            AgencyTypeCodes = new ObservableCollection<NAPS_AgencyTypeCodeViewModel>(this.AgencyTypeCodes),
                            States = new ObservableCollection<AIR_StateViewModel>(this.AirStates),                            
                        }
                      ).ToObservableCollection<NAPS_AgencyCodeViewModel>();