I have a page whose root element is a Grid named Root.
I have many controls like TextBlock, TextBox, Grid, Rectangle, Border etc... who are children of Root.
Now, I want to have a MouseDown or PreviewMouseDown or Click on Root to find the name of the element on which I clicked.
So, how can I get the name of the element on which I clicked / mousedown ?
If I can get a solution that uses Tunneling, It will be much easier.
RoutedEventArgs has property Source that Gets or sets a reference to the object that raised the event. Try this
xaml.cs
private void Grid_MouseDown_1(object sender, MouseButtonEventArgs e)
{
var mouseWasDownOn = e.Source as FrameworkElement;
if (mouseWasDownOn != null)
{
string elementName = mouseWasDownOn.Name;
}
}
xaml
<Grid PreviewMouseDown="Grid_MouseDown_1">
<StackPanel>
<ComboBox ItemsSource="{Binding ItemsSource}"/>
<Button Content="ok"/>
</StackPanel>
</Grid>
Khushi sara project stackoverflow walon se he karana hai kya :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With