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.
Wpf Custom Control Expose Custom Event within Control Property Window
BigH61
581
Reputation points
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.
1 answer
Sort by: Most helpful
-
BigH61 581 Reputation points
2022-01-05T15:36:45.403+00:00