How can I make MarkupExtenstion for button click event in WPF 4.5?

前端 未结 1 790
离开以前
离开以前 2021-01-16 19:23

I want to create the MarkupExtension to provide support for Button click event. I have got an article for PreviewKeyDown here. I tried to modify it for Click but unable to d

1条回答
  •  攒了一身酷
    2021-01-16 19:53

    Looks like its trying to cast MethodInfo as EventInfo, the returned target property could be a MethodInfo or EventInfo you will have to check this before createing the delegate

    Try this

        public class Call : MarkupExtension
        {
            public string ActionName { get; set; }
            public Call(string actionName) { ActionName = actionName; }
    
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                IProvideValueTarget targetProvider = serviceProvider
                    .GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
                if (targetProvider == null)
                    throw new InvalidOperationException(@"The CallAction extension 
                    can't retrieved the IProvideValueTarget service.");
    
                var target = targetProvider.TargetObject as FrameworkElement;
                if (target == null)
                    throw new InvalidOperationException(@"The CallAction extension 
                        can only be used on a FrameworkElement.");
    
                if (targetProvider.TargetProperty is MethodInfo)
                {
                    var targetEventAddMethod = targetProvider.TargetProperty as MethodInfo;
                    if (targetEventAddMethod != null)
                    {
                        ParameterInfo[] pars = targetEventAddMethod.GetParameters();
                        Type delegateType = pars[1].ParameterType;
                        MethodInfo methodInfo = this.GetType().GetMethod("MyProxyHandler", BindingFlags.NonPublic | BindingFlags.Instance);
                        return Delegate.CreateDelegate(delegateType, this, methodInfo); ;
                    }
    
                }
                else if (targetProvider.TargetProperty is EventInfo)
                {
                    var targetEventInfo = targetProvider.TargetProperty as EventInfo;
                    if (targetEventInfo != null)
                    {
                        Type delegateType = targetEventInfo.EventHandlerType;
                        MethodInfo methodInfo = this.GetType().GetMethod("MyProxyHandler", BindingFlags.NonPublic | BindingFlags.Instance);
                        return Delegate.CreateDelegate(delegateType, this, methodInfo);
                    }
                }
                return null;
            }
    
            void MyProxyHandler(object sender, EventArgs e)
            {
                FrameworkElement target = sender as FrameworkElement;
                if (target == null) return;
                var dataContext = target.DataContext;
                if (dataContext == null) return;
    
                //get the method on the datacontext from its name
                MethodInfo methodInfo = dataContext.GetType()
                    .GetMethod(ActionName, BindingFlags.Public | BindingFlags.Instance);
                methodInfo.Invoke(dataContext, null);
            }
        }
    

    0 讨论(0)
提交回复
热议问题