public static bool IsNavigable <T>(this ObjectBase obj, Expression <Func <T> > propertyExpression)
        {
            string       propertyName = PropertySupport.ExtractPropertyName(propertyExpression);
            PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName);

            return(propertyInfo.IsNavigable());
        }
        public override string this[string propertyName]
        {
            get
            {
                var baseResult = base[propertyName];
                if (baseResult != null)
                {
                    return(baseResult);
                }

                if (propertyName == PropertySupport.ExtractPropertyName(() => PlotSquare) && !IsValidAndPositiveDecimal(PlotSquare))
                {
                    return("Площадь участка введена некорректно");
                }

                if (propertyName == PropertySupport.ExtractPropertyName(() => HouseSquare) && !IsValidAndPositiveDecimal(HouseSquare))
                {
                    return("Площадь дома введена некорректно");
                }

                if (propertyName == PropertySupport.ExtractPropertyName(() => TotalFloor) && !IsValidAndPositiveInt(TotalFloor))
                {
                    return("Количество этажей введено некорректно");
                }

                return(null);
            }
        }
Example #3
0
        /// <summary>
        /// Sets value to the Model object's property
        /// </summary>
        /// <typeparam name="TObject"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="storageObject"></param>
        /// <param name="storageObjectPropertyExpression"></param>
        /// <param name="value"></param>
        /// <param name="propertyName"></param>
        /// <param name="userInput"></param>
        /// <returns></returns>
        protected bool SetProperty <TObject, TProperty>(TObject storageObject,
                                                        Expression <Func <dynamic> > storageObjectPropertyExpression, TProperty value,
                                                        [CallerMemberName] string propertyName = null, bool userInput = false)
        {
            if (userInput && !_userInput.Contains(propertyName))
            {
                _userInput.Add(propertyName);
            }

            var storagePropertyName = PropertySupport.ExtractPropertyName(storageObjectPropertyExpression);

            dynamic val = PropertySupport.GetPropertyValue <TObject, TProperty>(storageObject, storagePropertyName);

            if ((val as object)?.GetType().IsValueType ?? false && val != null && value != null)
            {
                if (val.Equals(value))
                {
                    return(false);
                }
            }
            else if (val == value)
            {
                return(false);
            }

            PropertySupport.SetPropertyValue(storageObject, storagePropertyName, value);

            {
                Invalidate(propertyName);
            }

            return(true);
        }
        /// <summary>
        /// Call to add specific validator to a model property
        /// </summary>
        protected PropertyValidationRule <TBindingModel> AddValidationFor <T>(Expression <Func <T> > propertyExpression, ValidationAttribute validator = null)
        {
            string propertyName = PropertySupport.ExtractPropertyName(propertyExpression);
            PropertyValidationRule <TBindingModel> result = AddValidationFor <T>(propertyName, validator);

            return(result);
        }
Example #5
0
        protected virtual void OnPropertyChanged <T>(Expression <Func <T> > propertyExpression)

        {
            var propertyName = PropertySupport.ExtractPropertyName(propertyExpression);

            OnPropertyChanged(propertyName);
        }
        /// <summary>
        /// 默认值为 null
        /// </summary>
        /// <typeparam name="X"></typeparam>
        /// <param name="propertyExpresssion"></param>
        /// <returns></returns>
        public object GetZobject <X>(Expression <Func <X> > propertyExpresssion)
        {
            var propertyName = PropertySupport.ExtractPropertyName(propertyExpresssion);
            var gt           = GetZ(propertyName);

            return(gt);
        }
        /// <summary>
        /// 默认值为 ObservableCollection<X>
        /// </summary>
        /// <typeparam name="X"></typeparam>
        /// <param name="propertyExpresssion"></param>
        /// <returns></returns>
        public ObservableCollection <X> GetZcollection <X>(Expression <Func <ObservableCollection <X> > > propertyExpresssion)
            where X : ObservableObject
        {
            var propertyName = PropertySupport.ExtractPropertyName(propertyExpresssion);
            var gt           = GetZ(propertyName);

            if (gt == null)
            {
                SetZ(propertyName, new ObservableCollection <X>());
            }

            gt = GetZ(propertyName);
            if (gt != null)
            {
                try
                {
                    return(gt as ObservableCollection <X>);
                }
                catch (Exception ex)
                {
                }
            }

            return(null);
        }
        private void OnSessionChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == PropertySupport.ExtractPropertyName(() => Session.CurrentFigureDescriptor))
            {
                var selectedVM = Sources
                                 .Select(s => { s.IsSelected = false; return(s); })
                                 .SelectMany(s => s.Figures)
                                 .Select(f => { f.IsSelected = false; return(f); })
                                 .SingleOrDefault(vm => vm.Model == Session.CurrentFigureDescriptor);

                if (selectedVM != null)
                {
                    selectedVM.IsSelected = true;
                }
            }
            else if (e.PropertyName == PropertySupport.ExtractPropertyName(() => Session.CurrentSource))
            {
                var selectedVM = Sources
                                 .Select(s => { s.IsSelected = false; return(s); })
                                 .SingleOrDefault(vm => vm.Model == Session.CurrentSource);

                if (selectedVM != null)
                {
                    selectedVM.IsSelected = true;
                }
            }
        }
Example #9
0
        /// <summary>
        /// Create a series from an enumerable with a projection function.
        /// </summary>
        /// <typeparam name="TSource">The type of the source data.</typeparam>
        /// <typeparam name="TResult">The type of the series.</typeparam>
        /// <param name="source">An enumerable of data from which the series should be built.</param>
        /// <param name="selectorExpression">A projection function.</param>
        /// <param name="isNullable">Optionally specify nullability for object types.</param>
        /// <returns>A series.</returns>
        public static ISeries CreateSeries <TSource, TResult>(this IEnumerable <TSource> source, Expression <Func <TSource, TResult> > selectorExpression, bool?isNullable = null)
        {
            var propertyName = PropertySupport.ExtractPropertyName(selectorExpression);
            var selector     = selectorExpression.Compile();

            return(CreateSeries(source, typeof(TResult), propertyName, x => selector((TSource)x), isNullable));
        }
 public static void RemoveListener <TProperty>(
     this INotifyPropertyChanged source,
     IWeakEventListener listener,
     Expression <Func <TProperty> > expression)
 {
     PropertyChangedEventManager.RemoveListener(source, listener, PropertySupport.ExtractPropertyName(expression));
 }
Example #11
0
        protected void RegisterParameter(Expression <Func <INotifyPropertyChanged> > parameterExpression)
        {
            var parameterName   = PropertySupport.ExtractPropertyName(parameterExpression);
            var parameterObject = parameterExpression.Compile()();

            parameterObject.PropertyChanged += (sender, args) => RaisePropertyChanged(parameterName);
        }
Example #12
0
        public void Export <T>(Expression <Func <T> > propertyExpression, string key = null)
        {
            key = key ?? PropertySupport.ExtractPropertyName(propertyExpression);

            if (_exportPropertyGetterDictionary.ContainsKey(key))
            {
                throw new ArgumentException(string.Format(Resources.DataContext_ExportHasBeenExported_Exception, propertyExpression), nameof(propertyExpression));
            }

            var getter = propertyExpression.Compile();

            _exportPropertyGetterDictionary[key] = getter;

            PropertyObserver.Observes(propertyExpression, () =>
            {
                if (!_importPropertySetterDictionary.TryGetValue(key, out var setterDelegates))
                {
                    return;
                }

                foreach (var setterDelegate in setterDelegates)
                {
                    var setter = (Action <T>)setterDelegate;
                    setter(getter());
                }
            });
        }
Example #13
0
 private void canvasBmp_MouseMove(object sender, MouseEventArgs e)
 {
     if (e.LeftButton == MouseButtonState.Pressed)
     {
         Point      curPos = e.GetPosition(imgCab);
         string     selPropName;
         OnOffGauge g = GetSelectedGauge(out selPropName);
         if (g != null && Math.Abs(curPos.X - lastDown.X) > MIN_MOUSE_MOVE_DRAG / scale && Math.Abs(curPos.Y - lastDown.Y) > MIN_MOUSE_MOVE_DRAG / scale)
         {
             int newX      = (int)(Math.Min(curPos.X, lastDown.X));
             int newY      = (int)(Math.Min(curPos.Y, lastDown.Y));
             int newWidth  = (int)(Math.Abs(curPos.X - lastDown.X));
             int newHeight = (int)(Math.Abs(curPos.Y - lastDown.Y));
             if (selPropName == PropertySupport.ExtractPropertyName(() => g.OnPosition))
             {
                 g.OnPosition.X = newX;
                 g.OnPosition.Y = newY;
             }
             else if (selPropName == PropertySupport.ExtractPropertyName(() => g.OffPosition))
             {
                 g.OffPosition.X = newX;
                 g.OffPosition.Y = newY;
             }
             else
             {
                 g.Position.X = newX;
                 g.Position.Y = newY;
             }
             g.Dimension.Width  = newWidth;
             g.Dimension.Height = newHeight;
             RefreshDisplayedRects();
         }
     }
 }
Example #14
0
        private void canvasBmp_MouseDown(object sender, MouseButtonEventArgs e)
        {
            lastDown = e.GetPosition(imgCab);
            string     selPropName;
            OnOffGauge g = GetSelectedGauge(out selPropName);

            if (g != null)
            {
                if (selPropName == null || selPropName == PropertySupport.ExtractPropertyName(() => g.Position))
                {
                    g.Position.X = (int)(lastDown.X);
                    g.Position.Y = (int)(lastDown.Y);
                }
                else if (selPropName == PropertySupport.ExtractPropertyName(() => g.OnPosition))
                {
                    g.OnPosition.X = (int)(lastDown.X);
                    g.OnPosition.Y = (int)(lastDown.Y);
                }
                else if (selPropName == PropertySupport.ExtractPropertyName(() => g.OffPosition))
                {
                    g.OffPosition.X = (int)(lastDown.X);
                    g.OffPosition.Y = (int)(lastDown.Y);
                }
                RefreshDisplayedRects();
            }
        }
        public static DataFrame PivotToDataFrame <TSource, TRow, TColumn, TValue>(
            this ICollection <TSource> source,
            Expression <Func <TSource, TRow> > rowSelector,
            Func <TSource, TColumn> columnSelector,
            Func <IEnumerable <TSource>, TValue> valueSelector)
        {
            var rowName = PropertySupport.ExtractPropertyName(rowSelector);
            var columns = source.Select(columnSelector).Distinct().ToList();
            var df      = new DataFrame(new IVector[] { new Vector <TRow>(rowName) }.Concat(columns.Select(x => x.ToString()).Select(x => new Vector <TValue>(x))));

            var rows = source.GroupBy(rowSelector.Compile())
                       .Select(rowGroup => new
            {
                Key    = rowGroup.Key,
                Values = columns.GroupJoin(rowGroup, c => c, columnSelector, (c, values) => valueSelector(values))
            });

            foreach (var row in rows)
            {
                var items = new [] { (object)row.Key }.Concat(row.Values.Cast <object>()).ToArray();
                df.Add(items);
            }

            return(df);
        }
        protected bool SetProperty<T>([CanBeNull] ref T backingField, [CanBeNull] T value, [NotNull] Expression<Func<T>> propertyExpression, [NotNull] Action<T, T> changeCallback)
        {
            Contract.Requires(propertyExpression != null);
            Contract.Requires(changeCallback != null);

            return SetProperty(ref backingField, value, PropertySupport.ExtractPropertyName(propertyExpression), changeCallback);
        }
Example #17
0
        private string GetErrorsFor <T>(Expression <Func <T> > propertyExpression)
        {
            var errors  = ErrorsContainer.GetErrors(PropertySupport.ExtractPropertyName(propertyExpression));
            var errStrs = errors.Where(r => !r.IsValid).Select(r => r.ErrorContent.ToString());
            var errStr  = string.Join(Environment.NewLine, errStrs);

            return(errStr);
        }
 [Ignore]    // cannot build the expression
 public void WhenExpressionRepresentsAPropertyWithNoGetMethod_ThenAnExceptionIsThrown()
 {
     ExceptionAssert.Throws <ArgumentException>(() =>
                                                PropertySupport.ExtractPropertyName(
                                                    Expression.Lambda <Func <int> >(
                                                        Expression.MakeMemberAccess(
                                                            null,
                                                            typeof(PropertySupportFixture).GetProperty("SetOnlyStaticProperty")))));
 }
        private void OnExecuteMicrophoneSelectedCommand(object param)
        {
            if (param is string)
            {
                this.CurrentMicrophone = param as string;
            }

            this.OnPropertyChanged(PropertySupport.ExtractPropertyName(() => this.IsCaptureDevicesSelected));
        }
Example #20
0
        protected void OnPropertyChanged <T>(Expression <Func <T> > propertyExpression)
        {
            var propertyName = PropertySupport.ExtractPropertyName(propertyExpression);

            if (propertyName != null)
            {
                OnPropertyChanged(propertyName);
            }
        }
Example #21
0
        public static Property <PropertyType> GetProperty <T, PropertyType>(this T owner, Expression <Func <T, PropertyType> > key, IPropertyBindingStrategy bindingStrategy = null, Func <PropertyType> getter = null, Action <T, PropertyType> setter = null)
        {
            Action <object, PropertyType> mySetter = null;

            if (setter != null)
            {
                mySetter = (o, v) => setter((T)o, v);
            }
            return(new Property <PropertyType>(owner, PropertySupport.ExtractPropertyName(key), bindingStrategy, getter, mySetter));
        }
        protected void ClearPropertyToValidate <T>(Expression <Func <T> > propertyExpression)
        {
            string       propertyName = PropertySupport.ExtractPropertyName(propertyExpression);
            PropertyInfo property     = ThisType.GetProperty(propertyName);

            if (property != null)
            {
                m_PropertiesToValidate.Remove(property);
            }
        }
        /// <summary>
        /// 默认值为 Empty
        /// </summary>
        /// <typeparam name="X"></typeparam>
        /// <param name="propertyExpresssion"></param>
        /// <returns></returns>
        public string GetZstring <X>(Expression <Func <X> > propertyExpresssion)
        {
            var propertyName = PropertySupport.ExtractPropertyName(propertyExpresssion);
            var gt           = GetZ(propertyName);

            if (gt == null)
            {
                return(string.Empty);
            }
            return(gt.ToString());
        }
Example #24
0
        public static Property <TPropertyType> GetProperty <T, TPropertyType>(
            this T owner,
            Expression <Func <T, TPropertyType> > key,
            IPropertyBindingStrategy bindingStrategy = null,
            Func <T, TPropertyType> getter           = null,
            Action <T, TPropertyType> setter         = null)
        {
            var propertyName = PropertySupport.ExtractPropertyName(key);

            return(owner.GetProperty(propertyName, bindingStrategy, getter, setter));
        }
Example #25
0
        protected void WhenStateUpdated <TProperty> (Expression <Func <TProperty> > property, Action action)
        {
            var name = PropertySupport.ExtractPropertyName(property);

            if (!_stateHandlers.ContainsKey(name))
            {
                _stateHandlers[name] = new List <Action> ();
            }

            _stateHandlers[name].Add(action);
        }
Example #26
0
        public void TrackProperties(params Expression <Func <object> >[] expressions)
        {
            foreach (var expression in expressions)
            {
                var propertyName  = PropertySupport.ExtractPropertyName(expression);
                var propertyValue = PropertySupport.GetPropertyValue <object, dynamic>(this, propertyName);

                _initialValues[propertyName] = propertyValue;
                OnValidate(propertyName);
            }
        }
            /// <summary>
            /// Initializes a new instance of the <see cref="T:TomsToolbox.ObservableCollections.ObservableExtensions.ObservableSelectImpl`2" /> class.
            /// </summary>
            /// <param name="sourceCollection">The source collection to wrap.</param>
            /// <param name="itemGeneratorExpression">The item generator to generate the wrapper for each item.</param>
            public ObservableSelectImpl([NotNull, ItemCanBeNull] IList <TSource> sourceCollection, [NotNull] Expression <Func <TSource, TTarget> > itemGeneratorExpression)
                : base(sourceCollection, itemGeneratorExpression.Compile())
            {
                _propertyName    = PropertySupport.TryExtractPropertyName(itemGeneratorExpression);
                _sourceReference = new WeakReference <IList <TSource> >(sourceCollection);

                if (_propertyName == null)
                {
                    return;
                }

                sourceCollection.ForEach(item => AttachItemEvents(item as INotifyPropertyChanged));
            }
            /// <summary>
            /// Initializes a new instance of the <see cref="T:TomsToolbox.ObservableCollections.ObservableExtensions.ObservableSelectImpl`2" /> class.
            /// </summary>
            /// <param name="sourceCollection">The source collection to wrap.</param>
            /// <param name="itemGeneratorExpression">The item generator to generate the wrapper for each item.</param>
            public ObservableSelectImpl([NotNull, ItemCanBeNull] IList <TSource> sourceCollection, [NotNull] Expression <Func <TSource, TTarget> > itemGeneratorExpression)
                : base(sourceCollection, itemGeneratorExpression.Compile())
            {
                _propertyName    = PropertySupport.TryExtractPropertyName(itemGeneratorExpression);
                _sourceReference = new TomsToolbox.Core.WeakReference <IList <TSource> >(sourceCollection);

                if (_propertyName == null)
                {
                    return;
                }

                _propertyChangeEventListeners = GeneratePropertyChangeEventListeners(sourceCollection);
            }
 public override string this[string columnName]
 {
     get
     {
         if (columnName == PropertySupport.ExtractPropertyName(() => Name))
         {
             if (String.IsNullOrWhiteSpace(Name))
             {
                 return(String.Format("Значение элемента справочника \"{0}\" не может быть пустым.", _DictionaryService.DictionaryName));
             }
         }
         return(null);
     }
 }
Example #30
0
        public bool CheckIsPropertyChanged(string propertyName)
        {
            var initialValue = _initialValues[propertyName];
            var actualValue  = PropertySupport.GetPropertyValue <object, dynamic>(this, propertyName);

            if (actualValue is IStorableBladeModel storableBladeModel)
            {
                return(storableBladeModel.IsChanged);
            }
            else
            {
                return(!PropertySupport.CompareHelper.AreEquals(actualValue, initialValue));
            }
        }