4

This is my first attempt to define my own RoutedEvent. When I debug this project, the OnMouseMove is reached as soon as I hover over the ellipse shape, but RaiseEvent does nothing. It's not even possible to step into RaiseEvent to see what's wrong (debugger just steps over).

Control:

using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows.Controls.Primitives;

namespace TestTrailer2
{
    public class Trailer2: Control
    {
        public static readonly RoutedEvent DragDeltaEvent;

        public event DragDeltaEventHandler DragDelta 
        {
            add {AddHandler(Trailer2.DragDeltaEvent, value);}
            remove {RemoveHandler(Trailer2.DragDeltaEvent, value);}
        }

        static Trailer2()
        {
            Trailer2.DragDeltaEvent = EventManager.RegisterRoutedEvent("DragDelta", RoutingStrategy.Bubble, typeof(DragDeltaEventHandler), typeof(Trailer2));
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            RaiseEvent(new DragDeltaEventArgs(0, 0));
        }
    }
}

Code behind:

using System;
using System.Windows;

namespace TestTrailer2
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        void Trailer2_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}

Markup:

<Window x:Class="TestTrailer2.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestTrailer2"
    Title="TestTrailer2" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type local:Trailer2}">
            <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:Trailer2}">
                    <Ellipse Width="100" Height="100" Fill="#AAD0D0DD"/>
                </ControlTemplate>
            </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <local:Trailer2 DragDelta="Trailer2_DragDelta">
    </local:Trailer2>
</Window>
oliver
  • 2,771
  • 15
  • 32
  • Are you [raising it correctly](https://stackoverflow.com/q/10144826/1997232)? I haven't done it myself, but it seems you have to pass parameter, which identify routed event, using one of overloads of [constructor](https://msdn.microsoft.com/en-us/library/ms587938(v=vs.110).aspx). – Sinatr Apr 05 '18 at 12:16
  • @Sinatr: I will try it. But the framework (Thumb control) does it exactly the same way as I do above. – oliver Apr 05 '18 at 12:19

1 Answers1

1

Problem is you are reusing DragDeltaEventArgs which originally belong to another routed event, Thumb.DragDeltaEvent. So when you do:

RaiseEvent(new DragDeltaEventArgs(0, 0));

What really is being raised is Thumb.DragDeltaEvent, not your custom event (note that you don't state which event to raise anywhere). That event is hardcoded in DragDeltaEventArgs constructor.

If you want to continue reusing it - you need to state your custom event explicitly:

var args = new DragDeltaEventArgs(0, 0);
// this is event being raised,
// by default for this routed args its `Thumb.DragDeltaEvent`
args.RoutedEvent = Trailer2.DragDeltaEvent;
RaiseEvent(args);

Or just use custom routed event args.

Evk
  • 98,527
  • 8
  • 141
  • 191
  • Aaaah, I have missed that DragDeltaEventArgs actually makes one reference to Thumb! At first glance it looked just like an innocent class with nothing special in it. Tricky... – oliver Apr 05 '18 at 12:25
  • Thank you so much. It works now. It's hard to believe that I hoped for RaiseEvent to do its job without telling it which event to raise!!! :-))) – oliver Apr 05 '18 at 12:32