-1

I would like to get the clicked UIElement's child element that is button. Maybe there is simple and short solution for this? I have searched for this answer awhile, but could't find solution that would be easy to understand and use. I will appreciate any kind of help related to this question.

Code that i have right now:

private new void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{

    if (sender == (sender as UIElement))
    {

        //TODO: getting controls name that is placed inside clicked UIElement

    }

}

Edit:

Wanted to mention that UIElement is ContentControl that is using ResourceDictionary template. My xaml code looks something like this

 <ContentControl Style="{StaticResource DesignerItemStyle}">
        <Button x:Name="btnAdd" Content="add function" IsHitTestVisible="True" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
</ContentControl>
Mr. Blond
  • 1,113
  • 2
  • 18
  • 41
  • 2
    it depends on what UIElement you mean here, such as if that UIElement is the owner of the event `MouseLeftButtonDown` which is attached with the handler, you can use either `sender` or `e.Source`, otherwise you may have to look into `e.OriginalSource`. – King King Jul 12 '14 at 23:30

1 Answers1

2

There are two properties in MouseButtonEventArgs which you can leverage for this purpose

Source

This property contains reference to the object that raised the event. example Button etc

OriginalSource

the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class, which may have been done to flatten composited element trees. example a Rectangle, Border or any template element inside the Button.

you can retrieve the Name for the element by casting OriginalSource to FrameworkElement or more appropriate

if the OriginalSource is not the desired element then you can retrieve the Logical parent of OriginalSource and that is more likely to be the desired element and retrieving Name remain same as above.

retrieve logical parent example

LogicalTreeHelper.GetParent(e.OriginalSource as DependencyObject);
pushpraj
  • 13,458
  • 3
  • 33
  • 50
  • 1
    Thank you for your answer @pushpraj . I changed this line of code that you gave to something like this and it worked! - `var childControl = LogicalTreeHelper.GetChildren(e.Source as DependencyObject);` Then to get that child controls name just used foreach loop. Great! – Mr. Blond Jul 13 '14 at 05:01
  • I just realized that this method only returns that child element's Content not name or other properties. Also when i cast to FrameworkElement that e.OriginalSource also no way to get other properties than Content. – Mr. Blond Jul 13 '14 at 06:08
  • You can define a strategy to cast to `Panel` to get its children or a `ContentControl` to get It's content depending on the type of object or other suitable types as needed. `FrameworkElement` is quite basic example which helps to get `Name` property. You may post a working sample of your app. May I suggest after looking at it. – pushpraj Jul 13 '14 at 06:15