XAML Code:
<Window x:Class="LearningWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Background="Azure">
<Grid x:Name="grdMain" Height="100" Width="500" HorizontalAlignment="Center" VerticalAlignment="Center" IsHitTestVisible="True" Background="Black" ToolTip="Grid">
<Grid.RowDefinitions></Grid.RowDefinitions>
<StackPanel x:Name="stkMain" Grid.Row="0" Height="80" Width="250" IsHitTestVisible="True" Background="Yellow" ToolTip="StackPanel">
<Button x:Name="btnClick" Content="Click Me" Height="50" Width="90" Background="Pink" Margin="10" ToolTip="Click Button" IsHitTestVisible="True"/>
</StackPanel>
</Grid>
</Window>
Code behind file:
using System.Windows;
using System.Windows.Input;
namespace LearningWPF {
public partial class MainWindow: Window {
public MainWindow() {
InitializeComponent();
grdMain.MouseDown += Grid_MouseDown;
stkMain.MouseDown += StackPanel_MouseDown;
btnClick.MouseDown += btnClick_MouseDown;
}
void btnClick_Click(object sender, RoutedEventArgs e) {
MessageBox.Show("Direct Event is getting fired");
}
private void btnClick_MouseDown(object sender, MouseButtonEventArgs e) {
MessageBox.Show("Button MouseDown Called");
}
private void StackPanel_MouseDown(object sender, MouseButtonEventArgs e) {
MessageBox.Show("StackPanel MouseDown Called");
}
private void Grid_MouseDown(object sender, MouseButtonEventArgs e) {
MessageBox.Show("Grid MouseDown Called");
}
private void Grid_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
MessageBox.Show("Grid Preview MouseDown Called");
}
private void StackPanel_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
MessageBox.Show("StackPanel Preview MouseDown Called");
}
private void btnClick_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
MessageBox.Show("Button Preview MouseDown Called");
}
}
}
