(Version: Avalon CTP 2004 November)
Both UIElement and ContentElement implement IInputElement interface, which defines RaiseEvent method. This post gives you an example of using it.
We start with a simple Xaml file:
<Window x:Class="AvalonApplication1.Window1"
xmlns="http://schemas.microsoft.com/2003/xaml" xmlns:x="Definition"
Text="AvalonApplication1">
<DockPanel>
<Button ID="btn1" Content="Button 1" Click="ButtonClick1"/>
<Button ID="btn2" Content="Button 2" Click="ButtonClick2"/>
</DockPanel>
</Window>
Two event handlers are defined as follows:
private void ButtonClick1(object sender, RoutedEventArgs args)
{
//Do Nothing
}
private void ButtonClick2(object sender, RoutedEventArgs args)
{
MessageBox.Show("Button2 Just Received Click Event");
}
So when you click on btn2, you see a MessageBox. But when you click on btn1, you do not see anything in particular.
Now we are going to add code into ButtonClick1 so that it will raise Click event on btn2. As a result, the same MessageBox shows up when you click on btn1. The code below makes use of RaiseEvent:
private void ButtonClick1(object sender, RoutedEventArgs args)
{
RoutedEventArgs evtArgs = new RoutedEventArgs();
evtArgs.SetRoutedEventID(Button.ClickEventID);
btn2.RaiseEvent(evtArgs);
}
Note we do not call ButtonClick2 directly. Instead, we raise the Click event on btn2. ButtonClick2 is called to handle that event.
I end this post with a question for my reader: What will happen if we change ButtonClick1 code again? Are you going to see a MessageBox, a ContextMenu , an Exception, or nothing at all?
private void ButtonClick1(object sender, RoutedEventArgs args)
{
RoutedEventArgs evtArgs = new RoutedEventArgs();
evtArgs.SetRoutedEventID(ContextMenu.OpenedEventID);
btn2.RaiseEvent(evtArgs);
}
(This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)