Question

In a WPF app I have I bunch of CustomControls inside a Grid. For processing Mouse clicks on them I use the MouseLeftButtonDown event of the Grid and in the event handler I check which CustomControl was clicked:

private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        FrameworkElement feSourceComm = e.Source as FrameworkElement;
        MyCustomControl SCurrentComm = new MyCustomControl();            
        try
        {
            SCurrentComm = (MyCustomControl)feSourceComm;
        }
        catch (Exception)
        {
...

The problem appeared when I placed all CustomControls in a UserControl and then inside the Grid. In this case approach doesn't work.
I checked the type of click source in each case by e.Source.GetType().ToString(); and get the following results:

When there are no problem (in case I put CustomControls in the Grid without UserControl)

MyProjectNamespace.MyCustomControl

When I put CustomControls in the UserControl and then in the Grid

MyProjectNamespace.UserControls.MyUserControlName

When I put CustomControls in the UserControl and then in the Grid and load the UserControl from external file by XamlReader.Load

System.Windows.Controls.UserControl

So, my question:
How to make CustomControls vizible as e.Source when they are inside of a UserControl?

Was it helpful?

Solution

e.OriginalSource will tell you which specific element the click happened on. if that is not your customcontrol, walk up the Parent chain of the OriginalSource until you find your customcontrol

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top