如何在MainWindow.xaml中公开我的自定义控件位于用户控件中的自定义控件事件和属性?

时间:2022-09-02 13:33:11

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.

我陷入了这样一种情况:我的自定义控件有一个Text属性和一个在OnApplyTemplate方法中使用按钮注册的事件处理程序。

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.

如何访问Mainwindow.xaml中使用的用户控件内的自定义控件的依赖项属性?我想访问Mainwindow.xaml中自定义控件的属性和事件。

1 个解决方案

#1


0  

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.

要获取在Mainwindow.xaml内部使用的用户控件内的自定义控件的属性,在这种情况下使属性类型为字符串(对于Text),并在用户控制中将新属性应用于自定义控件中的此属性。

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

要处理一个事件,你可以在自定义控件中创建自己的事件处理程序

public event MyEventHandler;

公共事件MyEventHandler;

u need delegate as well

你也需要代表

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

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

and then in custom control on Click event of button write

然后在按钮写入的Click事件的自定义控件中

  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:)

希望这个帮助:)

#1


0  

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.

要获取在Mainwindow.xaml内部使用的用户控件内的自定义控件的属性,在这种情况下使属性类型为字符串(对于Text),并在用户控制中将新属性应用于自定义控件中的此属性。

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

要处理一个事件,你可以在自定义控件中创建自己的事件处理程序

public event MyEventHandler;

公共事件MyEventHandler;

u need delegate as well

你也需要代表

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

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

and then in custom control on Click event of button write

然后在按钮写入的Click事件的自定义控件中

  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:)

希望这个帮助:)