public static BaseValueViewModel Create <TObject, TRole>(IViewModelFactory mdlFactory, IZetboxContext ctx, IFrozenContext frozenCtx, ViewModel parent, string label, IDataObject obj, Expression <Func <TObject, PartyRole> > property)
            where TRole : class, PartyRole
            where TObject : class, IDataObject
        {
            var me       = (MemberExpression)property.Body;
            var propInfo = (PropertyInfo)me.Member;

            var initialPartyRole = (PartyRole)propInfo.GetValue(obj, null);

            var mdl = new ObjectReferenceValueModel(label, "", false, false, (ObjectClass)NamedObjects.Base.Classes.Zetbox.Basic.Parties.Party.Find(frozenCtx));

            mdl.Value            = initialPartyRole != null ? initialPartyRole.Party : null;
            mdl.PropertyChanged += (s, e) =>
            {
                if (e.PropertyName == "Value")
                {
                    if (mdl.Value != null)
                    {
                        var party     = (Party)mdl.Value;
                        var partyRole = party.PartyRole.OfType <TRole>().SingleOrDefault();
                        if (partyRole == null)
                        {
                            partyRole       = ctx.Create <TRole>();
                            partyRole.Party = party;
                        }
                        propInfo.SetValue(obj, partyRole, null);
                    }
                    else
                    {
                        propInfo.SetValue(obj, null, null);
                    }
                }
            };
            return(mdlFactory.CreateViewModel <ObjectReferenceViewModel.Factory>().Invoke(ctx, parent, mdl));
        }
Exemplo n.º 2
0
        public MergeObjectsTaskViewModel(IViewModelDependencies appCtx, IZetboxContext dataCtx, ViewModel parent, IDataObject target, IDataObject source)
            : base(appCtx, dataCtx, parent)
        {
            if (target == null && source == null) throw new ArgumentException("Either source or target must not be null");

            ObjectClass = (source ?? target).GetObjectClass(FrozenContext);

            _targetMdl = new ObjectReferenceValueModel(MergeObjectsTaskViewModelResources.Target, MergeObjectsTaskViewModelResources.Target_Tooltip, false, false, ObjectClass);
            _targetMdl.Value = target;
            _targetMdl.PropertyChanged += _mdl_PropertyChanged;

            _sourceMdl = new ObjectReferenceValueModel(MergeObjectsTaskViewModelResources.Source, MergeObjectsTaskViewModelResources.Source_Tooltip, false, false, ObjectClass);
            _sourceMdl.Value = source;

            var ws = GetWorkspace() as IContextViewModel;
            if(ws == null)
            {
                throw new InvalidOperationException("A MergeObjectsTaskViewModel must be bound to a IContextViewModel workspace");
            }

            ws.Saving += OnSaving;
            ws.Saved += OnSaved;
        }
Exemplo n.º 3
0
        public static DialogCreator AddObjectReference(this DialogCreator c, object key, string label, Zetbox.App.Base.ObjectClass cls, IDataObject value = null, bool allowNullInput = false, bool isReadOnly = false, ControlKind requestedKind = null, ViewModelDescriptor vmdesc = null, string description = null, string helpText = null)
        {
            if (c == null) throw new ArgumentNullException("c");
            if (key == null) throw new ArgumentNullException("key");

            var mdl = new ObjectReferenceValueModel(label, description, allowNullInput, isReadOnly, cls);
            mdl.Value = value;
            mdl.HelpText = helpText;

            BaseValueViewModel vmdl;
            if (vmdesc != null)
                vmdl = c.ViewModelFactory.CreateViewModel<ObjectReferenceViewModel.Factory>(vmdesc).Invoke(c.DataContext, null, mdl);
            else
                vmdl = c.ViewModelFactory.CreateViewModel<ObjectReferenceViewModel.Factory>().Invoke(c.DataContext, null, mdl);

            if (requestedKind != null)
                vmdl.RequestedKind = requestedKind;

            c.Add(key, vmdl);
            return c;
        }