public SpeciesAvailableNameControl(TaxonViewModel taxon, User user)
            : base(taxon, user, "SpeciesAvailabeNames::")
        {
            InitializeComponent();

            txtReference.BindUser(user, LookupType.Reference);
            txtNameStatus.BindUser(user, PickListType.Phrase, "SAN Name Status", TraitCategoryType.Taxon);
            txtInstitution.BindUser(user, PickListType.Phrase, "Institution", TraitCategoryType.Taxon);
            txtSpecimen.BindUser(user, LookupType.Material);

            int taxaId = taxon.TaxaID.GetValueOrDefault(-1);
            SpeciesAvailableName data = Service.GetSpeciesAvailableName(taxaId) ?? new SpeciesAvailableName { BiotaID = taxaId };

            _model = new SpeciesAvailableNameViewModel(data);

            _model.DataChanged += changed => RegisterUniquePendingChange(new UpdateSanDatabaseCommand(_model.Model));

            cmbPrimaryType.SelectionChanged += (source, e) => {
                                                   var tstr = cmbPrimaryType.SelectedItem as string;
                                                   if (tstr != null && _SANTypeDataTypes.ContainsKey(tstr)) {
                                                       var typedata = _SANTypeDataTypes[tstr];
                                                       cmbSecondaryType.ItemsSource = typedata.SecondaryTypes;
                                                   }
                                               };

            if (taxon.TaxaID != null) {
                List<SANTypeDataType> santypes = Service.GetSANTypeDataTypes(taxon.TaxaID.Value);
                foreach (SANTypeDataType type in santypes) {
                    _SANTypeDataTypes[type.PrimaryType] = type;
                }

                cmbPrimaryType.ItemsSource = santypes.ConvertAll(st=>st.PrimaryType);
            }

            var tdlist = Service.GetSANTypeData(taxaId);
            _typeData = new ObservableCollection<SANTypeDataViewModel>(tdlist.ConvertAll(d => {
                var viewmodel = new SANTypeDataViewModel(d);
                viewmodel.DataChanged +=changed => {
                                            if (viewmodel.SANTypeDataID >= 0) {
                                                RegisterUniquePendingChange(new UpdateSANTypeDataCommand(viewmodel.Model));
                                            }
                                        };
                return viewmodel;
            }));
            lstTypeData.ItemsSource = _typeData;

            lstTypeData.SelectionChanged +=(source, e) => {

                gridTypeData.IsEnabled = lstTypeData.SelectedItem != null;

                var availableTypes = new List<string> {cmbPrimaryType.Text};
                var secondaries = cmbSecondaryType.ItemsSource as IEnumerable<string>;

                if (secondaries != null) {
                    availableTypes.AddRange(secondaries);
                }

                cmbType.ItemsSource = availableTypes;
                gridTypeData.DataContext = lstTypeData.SelectedItem;
            };

            DataContext = _model;

            gridTypeData.IsEnabled = false;

            this.BackgroundInvoke(() => {
                if (_typeData.Count > 0) {
                    lstTypeData.SelectedIndex = 0;
                }
            });
        }
 public void AddNewSANTypeData()
 {
     var td = new SANTypeData {SANTypeDataID = -1, BiotaID = Taxon.TaxaID.GetValueOrDefault(-1)};
     var viewModel = new SANTypeDataViewModel(td);
     viewModel.Museum = NextNewName("<New {0}>", _typeData, () => viewModel.Museum);
     _typeData.Add(viewModel);
     lstTypeData.SelectedItem = viewModel;
     RegisterPendingChange(new InsertSANTypeDataCommand(td));
 }