简体   繁体   中英

How do I expose custom control events and properties in MainWindow.xaml where my custom control is inside a User Control?

I am stuck in a situation where my custom control has one Text property and an event handler registered with a button in OnApplyTemplate method.

But when I put this custom control inside a user control and than I use this user control in a window where I am trying to access Custom control Text dependency property but not able to do so. Also trying to Fire click event from the button which is inside custom control, but when I click on the button the event does not fire. Nothing happens.

If anyone has found the similar problem and have resolved and know any solution to this .

How can I access the dependency property of custom control which is inside a user control which is used inside a Mainwindow.xaml ? I want to access the properties and events of custom control inside Mainwindow.xaml .

To get to properties of custom control which is inside a user control which is used inside a Mainwindow.xaml make properties in this case type of string (for Text ) and apply in user control your new property to this one in ur custom control.

To handle an event u can make in your custom control your own event handler like this

public event MyEventHandler;

u need delegate as well

public delegate void MyEventHandler(object sender, ChoosenIdsEventArgs e);

and then in custom control on Click event of button write

  protected void btn_Click(object sender, EventArgs e)
  {
   if(MyEventHandler != null)//check if u have this handler invoke on parent (user control in this case)
   {
       MyEventHandler(sender,e);
   }
  }

same thing can you do on your user control.

hope this help:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM