Exemplo n.º 1
0
        private void ComboBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var cb = sender as ComboBox;

            if ((e.Key == Key.Return ||
                 e.Key == Key.Enter) &&
                cb.Text != "")
            {
                bool duplicate = false;
                foreach (ChildVM vm in MyVM.ChildVMCollection)
                {
                    if (vm.Text == cb.Text)
                    {
                        cb.SelectedItem = vm;
                        duplicate       = true;
                        break;
                    }
                }

                if (duplicate)
                {
                    return;
                }

                // create a ChildM and corresponding ChildVM
                // (ChildVM inherits from ChildM)
                var cvm = new ChildVM()
                {
                    Text = cb.Text
                };
                MyVM.ChildMCollection.Insert(0, cvm);
                cb.SelectedItem = cvm;
            }
        }
Exemplo n.º 2
0
        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var cb = sender as ComboBox;

            //if (cb.SelectedItem is the VM with Style != Normal)

            ChildVM foundVM = null;

            foreach (ChildVM vm in MyVM.ChildVMCollection)
            {
                if (vm.Style != FontStyles.Normal &&
                    cb.SelectedItem == vm)
                {
                    foundVM = vm;
                    break;
                }
            }

            if (foundVM != null)
            {
                cb.Text   = "";
                e.Handled = true;
            }
        }
Exemplo n.º 3
0
        private void ChildMCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (SynchDisabled)
            {
                return;
            }
            SynchDisabled = true;

            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                foreach (object item in e.NewItems)
                {
                    var vmItem = new ChildVM((ChildM)item);

                    // the operation could have been either Insert or Add
                    if (0 <= e.NewStartingIndex &&
                        e.NewStartingIndex <= ChildMCollection.Count)
                    {
                        ChildVMCollection.Insert(e.NewStartingIndex, vmItem);
                    }
                    else     // e.NewStartingIndex < 0, so addition
                    {
                        ChildVMCollection.Insert(ChildMCollection.Count - 1, vmItem);
                    }
                }
                break;

            case NotifyCollectionChangedAction.Remove:
                foreach (object item in e.OldItems)
                {
                    // find VM objects that wrap the relevant model object and remove them
                    IEnumerable <ChildVM> query;
                    while ((query = from vm in ChildVMCollection
                                    where vm.Text == ((ChildM)item).Text
                                    select vm).Count() > 0)
                    {
                        ChildVM vmItem = query.First();
                        int     index  = ChildVMCollection.IndexOf(vmItem);
                        ChildVMCollection.Remove(vmItem);     // TODO: what if I implement the == or != operator?
                    }
                }
                break;

            case NotifyCollectionChangedAction.Reset:
                for (int i = ChildVMCollection.Count - 2; i >= 0; --i)
                {
                    ChildVMCollection.RemoveAt(i);
                }
                break;

            case NotifyCollectionChangedAction.Move:
                // TODO: handle multiple items
                ChildVMCollection.Move(e.OldStartingIndex, e.NewStartingIndex);
                break;

            case NotifyCollectionChangedAction.Replace:
                // TODO: handle multiple items
                ChildVMCollection[e.OldStartingIndex] = new ChildVM((ChildM)e.NewItems[0]);
                break;

            default:
                throw new NotImplementedException();
            }

            SynchDisabled = false;
        }