Exemplo n.º 1
0
 private static void OnPropertyChangedCommand(ICommandSource <PropertyChangedEventArgs> source, ICommandState state)
 {
     if (!source.EventArgs.PropertyName.In(state.GetValueOrDefault <string[]>(statePropertyNames)))
     {
         return;
     }
     state.GetValueOrDefault <Action>(stateHandler)?.Invoke();
 }
        private static void OnUpdatePropertyCommand(ICommandSource src, ICommandState state, object target)
        {
            string sourcePropertyName = state.GetValueOrDefault <string>(stateSourcePropertyName);

            if (sourcePropertyName == null)
            {
                Throw.InvalidOperationException(Res.ComponentModelMissingState(stateSourcePropertyName));
            }
            string targetPropertyName = state.GetValueOrDefault <string>(stateTargetPropertyName);

            if (targetPropertyName == null)
            {
                Throw.InvalidOperationException(Res.ComponentModelMissingState(stateTargetPropertyName));
            }

            object propertyValue         = null;
            bool   propertyValueObtained = false;

            if (src.EventArgs is PropertyChangedEventArgs e)
            {
                if (e.PropertyName != sourcePropertyName)
                {
                    return;
                }
                propertyValueObtained = e.TryGetNewPropertyValue(out propertyValue);
            }

            if (!propertyValueObtained)
            {
                object source = src.Source;
                propertyValue = source is IPersistableObject persistableSource && persistableSource.TryGetPropertyValue(sourcePropertyName, out propertyValue) ||
                                source is ICommandState stateSource && stateSource.TryGetValue(sourcePropertyName, out propertyValue)
                    ? propertyValue : Reflector.GetProperty(source, sourcePropertyName);
            }

            var formatValue = state.GetValueOrDefault <Func <object, object> >(stateFormatValue);

            if (formatValue != null)
            {
                propertyValue = formatValue.Invoke(propertyValue);
            }

            switch (target)
            {
            case IPersistableObject persistableTarget:
                persistableTarget.SetProperty(targetPropertyName, propertyValue);
                break;

            case ICommandState stateTarget:
                stateTarget[targetPropertyName] = propertyValue;
                break;

            default:
                Reflector.SetProperty(target, targetPropertyName, propertyValue);
                break;
            }
        }