Dessus

I am currently trying to implement an application where i require the ability to point all events to 1 method. That is simple enough if i declare all events that I want. For example:

Code Snippet

<Window x:Class="CaptureEventsSpike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CaptureEventsSpike" Height="300" Width="300"
Button.Click="GenericHandler"
>
<Grid>
<Button Height="23" HorizontalAlignment="Left" Margin="54,71,0,0" Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
<Button Height="23" HorizontalAlignment="Left" Margin="154,71,0,0" Name="button2" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>
</Window>


That works fine, but if i want to capture any/all events I don't want to have to put every possibility (there will be hundreds) in the winform/WPF. Is there anyway to obtain any event occuring on a Window or the application (wpf or not) no matter what the event is .

I would be looking for something like:

Code Snippet

<Window x:Class="CaptureEventsSpike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="CaptureEventsSpike" Height="300" Width="300"
OnAnyEvent="GenericHandler"
>
<Grid>
<Button Height="23" HorizontalAlignment="Left" Margin="54,71,0,0" Name="button1" VerticalAlignment="Top" Width="75">Button</Button>
<Button Height="23" HorizontalAlignment="Left" Margin="154,71,0,0" Name="button2" VerticalAlignment="Top" Width="75">Button</Button>
</Grid>
</Window>


I would like it if possible that any events that occur on any subcontrols of the window are put into an event loop(c++ style).

Thanks
Richard



Re: Windows Presentation Foundation (WPF) Generic event handler

Ivan Shumilin

public Window1()
{
InitializeComponent();

foreach (RoutedEvent re in EventManager.GetRoutedEvents())
{
AddHandler(re, new RoutedEventHandler(GenericHandler), true);
}
}

void GenericHandler(object sender, RoutedEventArgs e)
{
Title = e.ToString();
}




Re: Windows Presentation Foundation (WPF) Generic event handler

Dessus

Thanks heaps. That is what I was looking for.