Search code examples
c#wpfcaliburn.microarcgis-runtime

How can I better control action bubbling with caliburn micro?


I have a Thumb control which users should be able to drag around the UI. When I try dragging the control I get an exception "No target found for method Thumb_DragDelta". I think the problem may lie in the way I have the method written in the viewmodel, but I am not sure. Also, the method is not very good MVVM because it references the view directly, but again, not sure how to make it better. I am very new to Caliburn Micro and programming in general. Any help is appreciated!

The thumb control in the view:

        <Thumb cal:Message.Attach="[Event DragDelta] = [Action Thumb_DragDelta($view)]"
               Background="blue"
               HorizontalAlignment="Left" 
               Width="20"/>

And the corresponding method in the viewmodel:

        private Thumb _thumb;

        public Thumb MyThumb
        {
            get
            {
                return _thumb;
            }
            set {
                _thumb = value;
                NotifyOfPropertyChange(() => MyThumb);
            }
        }

        public void Thumb_DragDelta(Views.NaturalDisasterView view, DragDeltaEventArgs e)
        {
            try
            {
                var transform = (TranslateTransform)MyThumb.RenderTransform;
                transform.X = Math.Max(0, Math.Min(transform.X + e.HorizontalChange, view.ActualWidth - MyThumb.ActualWidth));
                view.AfterScene.Clip = new RectangleGeometry() { Rect = new Rect(0, 0, transform.X, view.ActualHeight) };
            }

            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);

            }
        }

Solution

  • Given your current XAML, the valid signature for the Thumb_DragDelta method in the view model would be the following, i.e. it accepts only a single argument which is a reference to the view that is bound to the view model:

    public void Thumb_DragDelta(FrameworkElement view)
    {
        //...
    }
    

    If you want the EventArgs as well, you should also pass $eventArgs:

    <Thumb cal:Message.Attach="[Event DragDelta] = [Action Thumb_DragDelta($view, $eventArgs)]"
                   Background="blue"
                   HorizontalAlignment="Left" 
                   Width="20"/>