Wpf Custom Control Expose Custom Event within Control Property Window

BigH61 581 Reputation points
2022-01-05T14:09:50.547+00:00

Further to an answer provided to an earlier question wpf-custom-control-switch-controltemplate-on-trigg.html

I would like to expose a click event for the ToggleButton within my CustomControl.
I have tried a RoutedEvent as below but I need to expose the Click event within the Controls Property Window so that the user is free to utilise the Click event like any other control event.

public class MyTextBox : Control  
    {        
        static MyTextBox()  
        {  
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(typeof(MyTextBox)));  
        }  
  
        private ToggleButton _tgButton;  
  
        // events exposed to container  
        public static readonly RoutedEvent OnSearchButtonClickedEvent =  
            EventManager.RegisterRoutedEvent("OnSearchButtonClicked", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(MyTextBox));  
  
        // get the button objects as the templete is applied and add click event handlers  
        public override void OnApplyTemplate()  
        {  
            base.OnApplyTemplate();  
            _tgButton = GetTemplateChild("tgButton") as ToggleButton;  
            if (_tgButton != null) _tgButton.Click += SearchButtonClicked;  
        }  
  
        // expose and raise 'OnSearchButtonClicked' event  
        public event RoutedEventHandler OnSearchButtonClicked  
        {  
            add { AddHandler(OnSearchButtonClickedEvent, value); }  
            remove { RemoveHandler(OnSearchButtonClickedEvent, value); }  
        }  
  
        private void SearchButtonClicked(object sender, RoutedEventArgs e)  
        {  
            RaiseEvent(new RoutedEventArgs(OnSearchButtonClickedEvent));  
        }  
    }  

Thank you in advance for any assistance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,676 questions
{count} votes

1 answer

Sort by: Most helpful
  1. BigH61 581 Reputation points
    2022-01-05T15:36:45.403+00:00

    The above RoutedEvent does actually works as required, exposing the Click event within the Controls Property Window.
    For what ever reason (I don’t know) it required me to close Visual Studio and re-open the project. This is despite having running Build -> Clean Solution followed by Build -> Rebuild Solution.

    0 comments No comments