question

essamce avatar image
essamce asked ·

events in custom control in wpf

I've made this user control and it works well, I'm trying to covert it to a Custom Control, but I can't hook up my handler to those 3 events.
any help will be appreciated.

 <Canvas x:Class="SMFU.UserControls.Public.MovableChildCanvas"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:SMFU.UserControls.Public"
         mc:Ignorable="d" 
            
         Height="auto" Width="auto"
            
         Background="Transparent"
         MouseLeftButtonDown="canvas_MouseLeftButtonDown"
         MouseLeftButtonUp="canvas_MouseLeftButtonUp"
         MouseMove="canvas_MouseMove"  >
 </Canvas>
windows-wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-MSFT avatar image
DaisyTian-MSFT answered ·

You can declare and register routing events in your custom control, and override the event method:

 class MyButton : Button
     {
         public static readonly RoutedEvent MyEventRoutedEvent =
         EventManager.RegisterRoutedEvent("MyEvent", RoutingStrategy.Bubble, typeof(EventHandler<MyEventRoutedEventArgs>), typeof(MyButton));
         //CLR
         public event RoutedEventHandler MyEvent
         {
             add { this.AddHandler(MyEventRoutedEvent, value); }
             remove { this.RemoveHandler(MyEventRoutedEvent, value); }
         }
    
         protected override void OnClick()
         {
             base.OnClick();
    
             MyEventRoutedEventArgs args = new MyEventRoutedEventArgs(MyEventRoutedEvent, this);
             args.ClickTime = DateTime.Now;
             this.RaiseEvent(args);
         }
     }

Declare the MyEventRoutedEventArgs:

  class MyEventRoutedEventArgs : RoutedEventArgs
     {
         public MyEventRoutedEventArgs(RoutedEvent routedEvent, object source) : base(routedEvent, source) { }
    
         public DateTime ClickTime { get; set; }
     }

Then you can use it in the control like local:MyEventRoutedEvent.MyEvent="Event_MyEvent"

3 comments Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

hi @DaisyTian-MSFT thanks for replying, I just want to hook up a method in custom control class itself in my customControls library not in the target project, and why I have to do all these stuff with events that already exists:MouseLeftButtonDown, MouseLeftButtonUp, MouseMove. mycustomControl inherited from Canvas which already has those events.


0 Votes 0 · ·

the solution is easy if i inheret mycustomControl from UserControl just one line in Xaml and my method in code behind(as my shown in my post code ).

0 Votes 0 · ·

You specify multiple events in xaml with the same handler to hook the three events in one:

 <Canvas.Style>
         <Style TargetType="Canvas">
             <EventSetter Event="MouseMove" Handler="Canvas_MouseEvent"/>
             <EventSetter Event="MouseLeftButtonDown" Handler="Canvas_MouseEvent"/>
             <EventSetter Event="MouseLeftButtonUp" Handler="Canvas_MouseEvent"/>
         </Style>
     </Canvas.Style>
0 Votes 0 · ·