none
Define Click Event in WPF Custom Control ?,

    Question

  • Hi,

    I am new to Wpf Custom Control library Project,I needed to Customize the Wpf Button as Glass Style Button.

    I have done ,styling the button as Glassstyle.But i am very Confused how to add Events in my Glassbutton in Wpf Custom library project (Vb.net)

    Please guide me ....how to Define Custom Event in my GlassButton


    Kartheeswaran Jeyakumar

    Monday, July 02, 2012 5:55 AM

Answers

  • Hi Jeyakumar,

    To add events to a custom control, you need declare a public static RoutedEvent in your class and use EventManager.RegisterRoutedEvent() method to register these events.

    I wrote an example here that add a Click event to the Custom GlassButton control.

      public class GlassButton : Control
        {
            static GlassButton()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(GlassButton), new FrameworkPropertyMetadata(typeof(GlassButton)));
         
            }
            public static RoutedEvent ClickEvent=
            EventManager.RegisterRoutedEvent("Click", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(GlassButton));
    
            public event RoutedEventHandler Click
            {
                add { AddHandler(ClickEvent, value); }
                remove { RemoveHandler(ClickEvent, value); }
            }
    
            protected virtual void OnClick()
            {
                RoutedEventArgs args = new RoutedEventArgs(ClickEvent, this);
    
                RaiseEvent(args);
    
            }
    
            protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
            {
                base.OnMouseLeftButtonUp(e);
    
                OnClick();
            }
            
        }
    }
    

    It is in C#, you can use conversion tool to convert to VB.net. http://www.developerfusion.com/tools/convert/csharp-to-vb/

    Have a nice day,


    Kee Poppy [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, July 03, 2012 8:14 AM