Beispiel #1
0
        public static string Validate(this ObservableObject vm, string propertyName)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentException("Invalid property name", propertyName);
            }

            string error   = string.Empty;
            var    value   = vm.GetType().GetProperty(propertyName).GetValue(vm);
            var    results = new List <ValidationResult>(1);
            var    result  = Validator.TryValidateProperty(
                value,
                new ValidationContext(vm, null, null)
            {
                MemberName = propertyName
            },
                results);

            if (!result)
            {
                var validationResult = results.First();
                error = validationResult.ErrorMessage;
            }
            return(error);
        }
        public static PlainCommand GetPlainCommand(this ObservableObject payLoad, CommandContainer commandContainer, string commandName)
        {
            string functionName = $"Can{commandName}";

            functionName = functionName.Replace("Async", "");
            MethodInfo?method = payLoad.GetPrivateMethod(commandName);

            if (method == null)
            {
                Type type = payLoad.GetType();
                throw new BasicBlankException($"Method with the name of {commandName} was not found  Type was {type.Name}");
            }
            MethodInfo?  fun = payLoad.GetPrivateMethod(functionName);
            PlainCommand output;

            if (fun != null)
            {
                output = new PlainCommand(payLoad, method, canExecuteM: fun, commandContainer);
            }
            else
            {
                PropertyInfo?pp = payLoad.GetPrivateProperty(functionName);
                output = new PlainCommand(payLoad, method, canExecute: pp, commandContainer);
            }
            return(output);
        }
Beispiel #3
0
        private void DisplayNonModal(ObservableObject viewModel, string title)
        {
            Type viewModelType = viewModel.GetType();
            PopupAssociatedViewAttribute attribute = viewModelType.GetCustomAttributes <PopupAssociatedViewAttribute>().FirstOrDefault();

            if (attribute == null)
            {
                throw new InvalidOperationException($"PopupAssociatedViewAttribute not found on {viewModelType}.");
            }
            FrameworkElement view = Activator.CreateInstance(attribute.ViewType) as FrameworkElement;

            if (view == null)
            {
                throw new InvalidOperationException($"Cannot create {attribute.ViewType} instance.");
            }
            view.DataContext = viewModel;
            PopupWindow modalPopupWindow = new PopupWindow
            {
                Owner = _mainWindow,
                Title = title,
                PopupContentPresenter =
                {
                    Content = view
                }
            };

            modalPopupWindow.Closed += (sender, args) => _mainWindow.IsEnabled = true;
            _mainWindow.IsEnabled    = false;
            modalPopupWindow.Show(); // TODO: ShowDialog ?
        }
Beispiel #4
0
        internal static bool HasObservableProperties([NotNull] ObservableObject observableObject)
        {
            var type = observableObject.GetType();

            var propertyAccessors = ObservableObjectTypeCache.TryGet(type);

            if (propertyAccessors != null)
            {
                return(propertyAccessors.Count > 0);
            }

            return(GetPropertiesAnnotatedAsObservable(type).Any(property => property.GetIndexParameters().Length == 0));
        }
Beispiel #5
0
        public static void Update(ObservableObject sender, VcProperties property, object newValue, Boolean isAxisChanged)
        {
            Boolean isDataPoint = sender.GetType().Equals(typeof(DataPoint));

            if (isDataPoint)
            {
                UpdateDataPoint(sender as DataPoint, property, newValue, isAxisChanged);
            }
            else
            {
                UpdateDataSeries(sender as DataSeries, property, newValue, isAxisChanged);
            }
        }
Beispiel #6
0
        public static void Update(ObservableObject sender, VcProperties property, object newValue, Boolean isAxisChanged)
        {
            Boolean isDataPoint = sender.GetType().Equals(typeof(DataPoint));

            if (isDataPoint)
            {
                DataSeries ds = (sender as DataPoint).Parent;

                if (ds != null && (property == VcProperties.YValue || property == VcProperties.XValue))
                    UpdateDataSeries(ds, property, newValue);
            }
            else
                UpdateDataSeries(sender as DataSeries, property, newValue);
        }
 public static void CopyTo <T>(this ObservableObject o, T dest)
 {
     foreach (PropertyInfo pi in o.GetType().GetProperties())
     {
         if (pi.GetCustomAttribute(typeof(AllowCopyAttribute)) != null)
         {
             var a = (AllowCopyAttribute)pi.GetCustomAttribute(typeof(AllowCopyAttribute));
             if (a.AllowCopy)
             {
                 if (pi.GetValue(o) != dest.GetType().GetProperty(pi.Name).GetValue(dest))
                 {
                     dest.GetType().GetProperty(pi.Name).SetValue(dest, pi.GetValue(o));
                 }
             }
         }
     }
 }
        public static void Update(ObservableObject sender, VcProperties property, object newValue, Boolean isAxisChanged)
        {
            Boolean isDataPoint = sender.GetType().Equals(typeof(DataPoint));

            if (isDataPoint)
            {
                DataSeries ds = (sender as DataPoint).Parent;

                if (ds != null && (property == VcProperties.YValue || property == VcProperties.XValue))
                {
                    UpdateDataSeries(ds, property, newValue);
                }
            }
            else
            {
                UpdateDataSeries(sender as DataSeries, property, newValue);
            }
        }
Beispiel #9
0
        private FrameworkElement CreatePopupView(ObservableObject viewModel)
        {
            Type viewModelType = viewModel.GetType();

            // Search in registered view-viewmodel
            Type viewType;

            if (!_associatedViewsByViewModels.TryGetValue(viewModelType, out viewType))
            // if not found in registered view-viewmodel, search attribute
            {
                PopupAssociatedViewAttribute attribute = viewModelType.GetCustomAttributes <PopupAssociatedViewAttribute>().FirstOrDefault();
                if (attribute == null)
                {
                    throw new InvalidOperationException($"PopupAssociatedView attribute not found on {viewModelType}.");
                }
                //// Search ClosePopupCommandAttribute and hook command
                //Type iCommandType = typeof(ICommand);
                //IEnumerable<FieldInfo> closeCommandFields = viewModelType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance).Where(x => x.GetCustomAttributes<ClosePopupCommandAttribute>().Any() && x.FieldType == iCommandType); // TODO: inheriting from ICommand ?
                //foreach (FieldInfo fieldInfo in closeCommandFields)
                //{
                //    ClosePopupCommandAttribute closePopupCommandAttribute = fieldInfo.GetCustomAttributes<ClosePopupCommandAttribute>().FirstOrDefault();
                //    PropertyInfo relatedProperty = viewModelType.GetProperty(closePopupCommandAttribute.RelatedProperty);
                //    ICommand originalCommand = relatedProperty.GetValue(viewModel) as ICommand;
                //    // TODO: check if RelayCommand<T>
                //    ICommand closedCommand = new RelayCommand<object>(o =>
                //    {
                //        Close(viewModel);
                //        originalCommand.Execute(o);
                //    });
                //    fieldInfo.SetValue(viewModel, closedCommand);
                //}
                //
                viewType = attribute.ViewType;
            }
            FrameworkElement view = Activator.CreateInstance(viewType) as FrameworkElement;

            if (view == null)
            {
                throw new InvalidOperationException($"Popup {viewType} instance is not a valid FrameworkElement.");
            }
            view.DataContext = viewModel;
            return(view);
        }
Beispiel #10
0
        public ObservableObjectInfo([NotNull] ObservableObject observableObject)
        {
            var type = observableObject.GetType();

            var propertyAccessors = ObservableObjectTypeCache.Register(
                type,
                () =>
            {
                var list = new List <PropertyAccessor>();

                var objectTypeName = type.Name;

                foreach (var property in GetPropertiesAnnotatedAsObservable(type))
                {
                    if (property.GetIndexParameters().Length == 0)
                    {
                        var propertyType = property.PropertyType;

                        if (propertyType.IsSubclassOf(typeof(CommandBase)))
                        {
                            if (propertyType.IsSubclassOf(typeof(ParameterlessCommand)) || propertyType == typeof(ParameterlessCommand))
                            {
                                list.Add(
                                    new ParameterlessCommandPropertyAccessor(
                                        property.Name,
                                        objectTypeName,
                                        CreateParameterlessCommandPropertyGetMethod(property)));
                            }
                            else
                            {
                                Debug.Assert(propertyType.IsGenericType);
                                Debug.Assert(
                                    propertyType.GetGenericTypeDefinition() == typeof(Command <>) ||
                                    propertyType.GetGenericTypeDefinition() == typeof(AsyncCommand <>) ||
                                    propertyType.GetGenericTypeDefinition() == typeof(ParameterizedCommand <>));

                                list.Add(
                                    new ParameterizedCommandPropertyAccessor(
                                        property.Name,
                                        objectTypeName,
                                        CreateParameterizedCommandPropertyGetMethod(property)));
                            }
                        }
                        else
                        {
                            list.Add(
                                new PropertyPropertyAccessor(
                                    property.Name,
                                    objectTypeName,
                                    CreatePropertyGetMethod(property),
                                    !propertyType.IsValueType));
                        }
                    }
                }

                return(list);
            });

            observableObjectReference = new WeakReference <ObservableObject>(observableObject);
            valueBags = propertyAccessors.ConvertAll(propertyAccessor => propertyAccessor.CreateValueBag(observableObject));
        }
Beispiel #11
0
        public static void Update(ObservableObject sender, VcProperties property, object newValue, Boolean isAxisChanged)
        {
            Boolean isDataPoint = sender.GetType().Equals(typeof(DataPoint));

            if (isDataPoint)
                UpdateDataPoint(sender as DataPoint, property, newValue);
            else
                UpdateDataSeries(sender as DataSeries, property, newValue);
        }
Beispiel #12
0
        /// <summary>
        /// Attach events to each and every individual face of Faces
        /// </summary>
        /// <param name="Object">Object where events to be attached</param>
        internal void AttachEvent2DataPointVisualFaces(ObservableObject Object)
        {   
            if (Parent.RenderAs == RenderAs.Pie || Parent.RenderAs == RenderAs.Doughnut || Parent.RenderAs == RenderAs.SectionFunnel || Parent.RenderAs == RenderAs.StreamLineFunnel)
            {
                if (Faces != null)
                {
                    if ((Parent.Chart as Chart).View3D)
                    {
                        foreach (FrameworkElement element in Faces.VisualComponents)
                        {
                            AttachEvents2Visual(Object, this, element);

                            if ((Chart as Chart).ChartArea != null && (Chart as Chart).ChartArea._isDefaultInteractivityAllowed)
                            {
                                element.MouseLeftButtonUp -= new MouseButtonEventHandler(Visual_MouseLeftButtonUp);
                                element.MouseLeftButtonUp += new MouseButtonEventHandler(Visual_MouseLeftButtonUp);
                            }
                        }
                    }
                    else
                    {   
                        AttachEvents2Visual(Object, this, Faces.Visual);

                        if ((Chart as Chart).ChartArea != null && (Chart as Chart).ChartArea._isDefaultInteractivityAllowed)
                        {
                            Faces.Visual.MouseLeftButtonUp -= new MouseButtonEventHandler(Visual_MouseLeftButtonUp);
                            Faces.Visual.MouseLeftButtonUp += new MouseButtonEventHandler(Visual_MouseLeftButtonUp);
                        }
                    }

                    if (this.ExplodeAnimation != null)
                    {   
                        this.ExplodeAnimation.Completed -= new EventHandler(ExplodeAnimation_Completed);
                        this.ExplodeAnimation.Completed += new EventHandler(ExplodeAnimation_Completed);
                    }

                    if (this.UnExplodeAnimation != null)
                    {
                        this.UnExplodeAnimation.Completed -= new EventHandler(UnExplodeAnimation_Completed);
                        this.UnExplodeAnimation.Completed += new EventHandler(UnExplodeAnimation_Completed);
                    }
                }
            }
            else if (Parent.RenderAs == RenderAs.StackedArea 
                || Parent.RenderAs == RenderAs.StackedArea100 || Parent.RenderAs == RenderAs.Line)
            {
                if (Parent.RenderAs != RenderAs.Line)
                {
                    if (Parent.Faces != null)
                    {
                        if (Object.GetType().Equals(typeof(DataPoint)))
                        {
                            foreach (FrameworkElement face in Parent.Faces.VisualComponents)
                                AttachEvents2AreaVisual(Object, this, face);
                        }
                    }
                }

                if (Marker != null)
                    AttachEvents2Visual(Object, this, Marker.Visual);
            }
            else if (Faces != null)
            {
                if (Parent.RenderAs == RenderAs.Bubble || Parent.RenderAs == RenderAs.Point || Parent.RenderAs == RenderAs.Stock || Parent.RenderAs == RenderAs.CandleStick || Parent.RenderAs == RenderAs.SectionFunnel || Parent.RenderAs == RenderAs.StreamLineFunnel)
                {
                    foreach (FrameworkElement face in Faces.VisualComponents)
                    {
                        AttachEvents2Visual(Object, this, face);
                    }
                }
                else
                {
                    AttachEvents2Visual(Object, this, Faces.Visual);

                    if (Marker != null)
                        AttachEvents2Visual(Object, this, Marker.Visual);

                    if (LabelVisual != null)
                        AttachEvents2Visual(Object, this, LabelVisual);

                }
            }
        }
Beispiel #13
0
        internal static void Update(ObservableObject sender, VcProperties property, object newValue, Boolean isAxisChanged)
        {
            Boolean isDataPoint;

            if (property == VcProperties.Bevel)
            {
                sender = (sender as DataPoint).Parent;
                isDataPoint = false;
            }
            else
                isDataPoint = sender.GetType().Equals(typeof(DataPoint));

            if (isDataPoint )
                UpdateDataPoint(sender as DataPoint, property, newValue, isAxisChanged);
            else
                UpdateDataSeries(sender as DataSeries, property, newValue);
        }
            public ViewModelBinding(ConsoleControl control, PropertyInfo controlProperty, ObservableObject viewModel, string observablePropertyName)
            {
                var viewModelObservableProperty = viewModel.GetType().GetProperty(observablePropertyName);

                if (viewModelObservableProperty.PropertyType != controlProperty.PropertyType &&
                    viewModelObservableProperty.PropertyType.IsSubclassOf(controlProperty.PropertyType) == false &&
                    viewModelObservableProperty.PropertyType.GetInterfaces().Contains(controlProperty.PropertyType) == false)
                {
                    throw new InvalidOperationException($"ViewModel property '{viewModel.GetType().FullName}.{observablePropertyName}' of type {viewModelObservableProperty.PropertyType.FullName} is not compatible with control property '{controlProperty.DeclaringType.FullName}.{controlProperty.Name}' of type {controlProperty.PropertyType.FullName} ");
                }

                viewModel.SynchronizeForLifetime(observablePropertyName, () =>
                {
                    var newValue = viewModelObservableProperty.GetValue(viewModel);
                    if (newValue == latestValue) return;
                    latestValue = newValue;
                    controlProperty.SetValue(control, newValue);
                }, control.LifetimeManager);

                control.SubscribeForLifetime(controlProperty.Name, () =>
                {
                    var newValue = controlProperty.GetValue(control);
                    if (newValue == latestValue) return;
                    latestValue = newValue;
                    viewModelObservableProperty.SetValue(viewModel, newValue);
                }, control.LifetimeManager);
            }