Пример #1
0
#pragma warning disable 1591
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            try
            {
                var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
                if (!(target.TargetObject is DependencyObject)) return this;

                var element = target.TargetObject as FrameworkElement;
                var targetProperty = target.TargetProperty as DependencyProperty;
                if (element == null || targetProperty == null) return null;

                var viewModel = GetDataContext(element);
                if (viewModel == null)
                {
                    element.DataContextChanged += (s, e) =>
                    {
                        viewModel = e.NewValue;
                        var command = new CommandForMethod(viewModel, _name, CanExecuteWhen, ParameterConverter);
                        element.SetValue(targetProperty, command);
                    };
                }
                else
                {
                    var command = new CommandForMethod(viewModel, _name, CanExecuteWhen, ParameterConverter);
                    return command;
                }
            }
            catch { }

            return null;
        }
Пример #2
0
#pragma warning disable 1591
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            try
            {
                var target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
                if (!(target.TargetObject is DependencyObject))
                {
                    return(this);
                }

                var element        = target.TargetObject as FrameworkElement;
                var targetProperty = target.TargetProperty as DependencyProperty;
                if (element == null || targetProperty == null)
                {
                    return(null);
                }

                var viewModel = GetDataContext(element);
                if (viewModel == null)
                {
                    element.DataContextChanged += (s, e) =>
                    {
                        viewModel = e.NewValue;
                        var command = new CommandForMethod(viewModel, _name, CanExecuteWhen, ParameterConverter);
                        element.SetValue(targetProperty, command);
                    };
                }
                else
                {
                    var command = new CommandForMethod(viewModel, _name, CanExecuteWhen, ParameterConverter);
                    return(command);
                }
            }
            catch { }

            return(null);
        }