Beispiel #1
0
        //modified to traverse through a contextmenu's PlacementTarget value
        public static void SetMethodBinding(ActionExecutionContext context)
        {
            DependencyObject currentElement = context.Source;

            while (currentElement != null)
            {
                if (Caliburn.Micro.Action.HasTargetSet(currentElement))
                {
                    var target = Message.GetHandler(currentElement);

                    if (target != null)
                    {
                        var method = ActionMessage.GetTargetMethod(context.Message, target);
                        if (method != null)
                        {
                            context.Method = method;
                            context.Target = target;
                            context.View   = currentElement;
                            return;
                        }
                    }
                    else
                    {
                        context.View = currentElement;
                        return;
                    }
                }

                if (currentElement is ContextMenu)
                {
                    currentElement = ((ContextMenu)currentElement).PlacementTarget ?? VisualTreeHelper.GetParent(currentElement);
                }
                else
                {
                    currentElement = VisualTreeHelper.GetParent(currentElement);
                }
            }

            if (context.Source.DataContext != null)
            {
                var target = context.Source.DataContext;
                var method = ActionMessage.GetTargetMethod(context.Message, target);

                if (method != null)
                {
                    context.Target = target;
                    context.Method = method;
                    context.View   = context.Source;
                }
                else
                {
                    if (target.GetType().GetMethods().Any(m => m.Name == context.Message.MethodName))
                    {
                        throw new Exception($"Non of the methods found for binding {context.Message.MethodName} match paramter count");
                    }
                }
            }
        }