Silverlight events are CLR events, and therefore are events can be handled using managed code.
It is same as handling basic CLR events, we need to attach event handlers
XAML Code
<Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
<StackPanel>
<TextBlock Name="textBlock1">Put the mouse over this text</TextBlock>
...
</StackPanel>
</Grid>
VB.NET
Private Sub LayoutRoot_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs e)
AddHandler textBlock1.MouseEnter, AddressOf textBlocks_MouseEnter
AddHandler textBlock1.MouseLeave, AddressOf textBlocks_MouseLeave
End Sub
Handling Events
Sub textBlock1_MouseEnter(ByVal sender As Object, ByVal e As MouseEventArgs) Handles textBlock1.MouseEnter
'....
End Sub
Sub textBlock1_MouseLeave(ByVal sender As Object, ByVal e As MouseEventArgs) Handles textBlock1.MouseLeave
'....
End Sub
Silverlight event-handler functions cannot be called with parameter values (even empty ones), considerable difference from event handler syntax in the HTML DOM.
Visual Studio and its XAML design surface generally promote the instance-handling technique instead of the Handles keyword. This is because establishing the event handler wiring in XAML is part of typical designer-developer workflow for Silverlight and WPF, and the Handles keyword technique is incompatible with wiring the event handlers in XAML.
C#.NET
In C#, the syntax is to use the += operator. You instantiate the handler by declaring a new delegate that uses the event handler method name.
void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
{
textBlock1.MouseEnter += new MouseEventHandler(textBlocks_MouseEnter);
textBlock1.MouseLeave += new MouseEventHandler(textBlocks_MouseLeave);
}
To answer your original question ,here is the solution
private void LayoutRoot_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var elements = VisualTreeHelper.FindElementsInHostCoordinates(e.GetPosition(this.LayoutRoot), this.LayoutRoot);
foreach (var element in elements) {
OnMouseButton(element, e);
}
}
private void OnMouseButton(object sender, MouseButtonEventArgs e)
{
}
Replied on Jan 5 2011 8:32AM
.