Files
docs-desktop/dotnet-desktop-guide/net/wpf/events/snippets/how-to-create-a-custom-routed-event/csharp/MainWindow.xaml.cs
T
Tris ShoresandAndy De George 51977cea61 Content update - How to create a custom routed event (user story 1878475) (#1289)
* Add article, redirects, toc

* Update dotnet-desktop-guide/net/wpf/events/how-to-create-a-custom-routed-event.md

Co-authored-by: Andy (Steve) De George <[email protected]>

* Update dotnet-desktop-guide/net/wpf/events/snippets/how-to-create-a-custom-routed-event/csharp/CodeSample.csproj

Co-authored-by: Andy (Steve) De George <[email protected]>

* Update dotnet-desktop-guide/net/wpf/events/snippets/how-to-create-a-custom-routed-event/vb/CodeSampleVb.vbproj

Co-authored-by: Andy (Steve) De George <[email protected]>

* Update dotnet-desktop-guide/net/wpf/events/snippets/how-to-create-a-custom-routed-event/WpfControlLibraryCsharp/WpfControlLibrary.csproj

Co-authored-by: Andy (Steve) De George <[email protected]>

* Use local project references

Co-authored-by: Andy (Steve) De George <[email protected]>
2022-02-10 14:49:46 -08:00

43 lines
1.7 KiB
C#

using System.Diagnostics;
using System.Windows;
namespace CodeSample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// Assign an event handler to the CustomButton using the '+=' operator.
// customButton.ConditionalClick += Handler_ConditionalClick;
// Assign an event handler to the CustomButton using the AddHandler method.
// customButton.AddHandler(WpfControl.CustomButton.ConditionalClickEvent,
// new RoutedEventHandler(Handler_ConditionalClick));
// Assign an event handler to the StackPanel using the AddHandler method.
// StackPanel1.AddHandler(WpfControl.CustomButton.ConditionalClickEvent,
// new RoutedEventHandler(Handler_ConditionalClick));
}
//<EventHandler>
// The ConditionalClick event handler.
private void Handler_ConditionalClick(object sender, RoutedEventArgs e)
{
string senderName = ((FrameworkElement)sender).Name;
string sourceName = ((FrameworkElement)e.Source).Name;
Debug.WriteLine($"Routed event handler attached to {senderName}, " +
$"triggered by the ConditionalClick routed event raised on {sourceName}.");
}
// Debug output when CustomButton is clicked:
// Routed event handler attached to CustomButton,
// triggered by the ConditionalClick routed event raised on CustomButton.
// Routed event handler attached to StackPanel1,
// triggered by the ConditionalClick routed event raised on CustomButton.
//</EventHandler>
}
}