Exemplo n.º 1
0
        private CollectionBoxViewModel createViewModel(StackPanel control, string parent, string name, Type type, object[] attributes, IList member, bool index1)
        {
            Type elementType = ReflectionExt.GetBaseTypeArg(typeof(IList <>), type, 0);

            CollectionBoxViewModel mv = new CollectionBoxViewModel(new StringConv(elementType, ReflectionExt.GetPassableAttributes(1, attributes)));

            mv.Index1 = index1;
            //add lambda expression for editing a single element
            mv.OnEditItem += (int index, object element, CollectionBoxViewModel.EditElementOp op) =>
            {
                string       elementName = name + "[" + index + "]";
                DataEditForm frmData     = new DataEditForm();
                frmData.Title = DataEditor.GetWindowTitle(parent, elementName, element, elementType, ReflectionExt.GetPassableAttributes(1, attributes));

                DataEditor.LoadClassControls(frmData.ControlPanel, parent, elementName, elementType, ReflectionExt.GetPassableAttributes(1, attributes), element, true);

                frmData.SelectedOKEvent += () =>
                {
                    element = DataEditor.SaveClassControls(frmData.ControlPanel, elementName, elementType, ReflectionExt.GetPassableAttributes(1, attributes), true);
                    op(index, element);
                    frmData.Close();
                };
                frmData.SelectedCancelEvent += () =>
                {
                    frmData.Close();
                };

                control.GetOwningForm().RegisterChild(frmData);
                frmData.Show();
            };

            mv.LoadFromList(member);
            return(mv);
        }
Exemplo n.º 2
0
        public override IList SaveWindowControls(StackPanel control, string name, Type type, object[] attributes)
        {
            int controlIndex = 0;

            controlIndex++;
            IControl lbxValue         = control.Children[controlIndex];
            CollectionBoxViewModel mv = (CollectionBoxViewModel)lbxValue.DataContext;

            return(mv.GetList(type));
        }
Exemplo n.º 3
0
        public override ITypeDict SaveWindowControls(StackPanel control, string name, Type type, object[] attributes)
        {
            CollectionBox lbxValue = (CollectionBox)control.Children[1];

            ITypeDict member              = (ITypeDict)Activator.CreateInstance(type);
            CollectionBoxViewModel mv     = (CollectionBoxViewModel)lbxValue.DataContext;
            List <object>          states = (List <object>)mv.GetList(typeof(List <object>));

            for (int ii = 0; ii < states.Count; ii++)
            {
                member.Set(states[ii]);
            }
            return(member);
        }
Exemplo n.º 4
0
        public override Array SaveWindowControls(StackPanel control, string name, Type type, object[] attributes)
        {
            int controlIndex = 0;

            //TODO: 2D array grid support
            //if (type.GetElementType().IsArray)

            controlIndex++;
            IControl lbxValue              = control.Children[controlIndex];
            CollectionBoxViewModel mv      = (CollectionBoxViewModel)lbxValue.DataContext;
            List <object>          objList = (List <object>)mv.GetList(typeof(List <object>));

            Array array = Array.CreateInstance(type.GetElementType(), objList.Count);

            for (int ii = 0; ii < objList.Count; ii++)
            {
                array.SetValue(objList[ii], ii);
            }

            return(array);
        }
Exemplo n.º 5
0
        public override void LoadWindowControls(StackPanel control, string parent, string name, Type type, object[] attributes, IList member)
        {
            LoadLabelControl(control, name);

            Type elementType = ReflectionExt.GetBaseTypeArg(typeof(IList <>), member.GetType(), 0);

            CollectionBox lbxValue = new CollectionBox();

            lbxValue.MaxHeight = 180;
            CollectionBoxViewModel mv = new CollectionBoxViewModel(new StringConv(elementType, ReflectionExt.GetPassableAttributes(1, attributes)));

            lbxValue.DataContext = mv;

            //add lambda expression for editing a single element
            mv.OnEditItem += (int index, object element, CollectionBoxViewModel.EditElementOp op) =>
            {
                string       elementName = name + "[" + index + "]";
                DataEditForm frmData     = new DataEditForm();
                frmData.Title = DataEditor.GetWindowTitle(parent, elementName, element, elementType, ReflectionExt.GetPassableAttributes(1, attributes));

                //TODO: make this a member and reference it that way
                DataEditor.LoadClassControls(frmData.ControlPanel, parent, elementName, elementType, ReflectionExt.GetPassableAttributes(1, attributes), element, true);

                frmData.SelectedOKEvent += async() =>
                {
                    object newElement = DataEditor.SaveClassControls(frmData.ControlPanel, elementName, elementType, ReflectionExt.GetPassableAttributes(1, attributes), true);

                    bool itemExists = false;

                    List <object> states = (List <object>)mv.GetList(typeof(List <object>));
                    for (int ii = 0; ii < states.Count; ii++)
                    {
                        //ignore the current index being edited
                        //if the element is null, then we are editing a new object, so skip
                        if (ii != index || element == null)
                        {
                            if (states[ii].Equals(newElement))
                            {
                                itemExists = true;
                            }
                        }
                    }

                    if (itemExists)
                    {
                        await MessageBox.Show(control.GetOwningForm(), "Cannot add duplicate states.", "Entry already exists.", MessageBox.MessageBoxButtons.Ok);
                    }
                    else
                    {
                        op(index, newElement);
                        frmData.Close();
                    }
                };
                frmData.SelectedCancelEvent += () =>
                {
                    frmData.Close();
                };

                control.GetOwningForm().RegisterChild(frmData);
                frmData.Show();
            };

            List <object> states = new List <object>();

            foreach (object state in member)
            {
                states.Add(state);
            }
            mv.LoadFromList(states);
            control.Children.Add(lbxValue);
        }