Exemple #1
0
        public static PropertyCommandParameter <TCustomParameter, TValue> Cast(object parameter)
        {
            var parameterToCast = parameter as PropertyCommandParameter <object, object>;

            if (parameterToCast == null)
            {
                throw new InvalidCastException(string.Format("Failed to case {0} to {1}",
                                                             parameter.GetType(), typeof(PropertyCommandParameter <object, object>)));
            }

            var castedParameter = new PropertyCommandParameter <TCustomParameter, TValue>(
                (TCustomParameter)parameterToCast.CustomParameter,
                parameterToCast.Property,
                (TValue)parameterToCast.Value);

            return(castedParameter);
        }
Exemple #2
0
        protected override void InitializeCore(FrameworkElement source)
        {
            DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(Property, source.GetType());

            descriptor.AddValueChanged(source, (s, e) =>
            {
                CommandParameter <object> parameter = new PropertyCommandParameter <object, object>(
                    CustomParameter, Property, source.GetValue(Property));

                object value = Value;
                if (descriptor.Converter.CanConvertFrom(typeof(string)))
                {
                    value = descriptor.Converter.ConvertFromString(Value);
                }

                if (object.Equals(source.GetValue(Property), value))
                {
                    ExecuteCommand(parameter);
                }
            });
        }