---
title: "How to execute method in parent user control from child in wpf?"  
description: "How to execute method in parent user control from child in wpf?"  
author: "Anonymous User"  
published: 2014-11-14  
updated: 2014-11-15  
canonical: https://www.mindstick.com/forum/12587/how-to-execute-method-in-parent-user-control-from-child-in-wpf  
category: "wpf"  
tags: ["c#", "xaml"]  
reading_time: 1 minute  

---

# How to execute method in parent user control from child in wpf?

I have a [user control](https://www.mindstick.com/forum/294/problem-in-access-the-control-inside-the-user-control) named Display in MainWindow.xaml. Display user control itself has a [custom](https://www.mindstick.com/blog/12298/use-custom-writing-services-to-get-through-the-finals) [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) [Workspace](https://www.mindstick.com/blog/304118/transform-your-workspace-tips-for-enhancing-focus-and-productivity).cs in it like this:

```
<UserControl x:Class="Program.Main.Controls.Display.Display"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:workspaces="clr-namespace:Program.Workspaces">    <Border>        <Grid>            <workspaces:Workspace
x:Name="MyWorkspace" ClipToBounds="True"/>        </Grid>    </Border></UserControl>
```

In my custom class that is Worskspace.cs I'm rising a MouseLeftButtonDown [event](https://www.mindstick.com/articles/167719/marquee-hire-benefits-of-event-lighting-hire-london), I want to [execute](https://www.mindstick.com/interview/49/how-can-i-execute-a-php-script-using-command-line) the ThisCommandCameFromWorkspace [method](https://www.mindstick.com/forum/166/webservice-method) in Display.xaml.cs when rising this event.

What is the best [approach](https://www.mindstick.com/interview/985/what-are-the-approaches-that-you-will-follow-for-making-a-program-very-efficient) to do this? And What is the easiest?

**Updated:**

In my Workspace.cs I have

```
public event Action MyEventHandler;private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)    {            if (MyEventHandler!= null)                MyEventHandler();    }
```

Now I want to [capture](https://answers.mindstick.com/qa/30907/which-phone-is-better-between-billion-capture-plus-and-xiaomi-mi-a1-and-moto-g5s-plus) this in Display user control and execute a method of its code-behind.

## Replies

### Reply by Elena Glibart

I'd suggest not to use Action as event delegate type, but instead use the common type EventHandler. And don't call it MyEventHandler as it is not a handler, but just the event:

```
public event EventHandler MyEvent;private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e){    if (MyEvent != null)    {        MyEvent(this, EventArgs.Empty);    }}
```

Now you would easily attach a handler for the event in the Display's XAML (supported by Intellisense in Visual Studio):

<workspaces:Workspace ... MyEvent="Display_MyEvent"/>


---

Original Source: https://www.mindstick.com/forum/12587/how-to-execute-method-in-parent-user-control-from-child-in-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
