简体   繁体   English

Fire父事件基于WPF中的子控件事件

[英]Fire Parent event based upon child control event in WPF

I have a user control that contains button as follows 我有一个包含按钮的用户控件,如下所示

<UserControl x:Class="MyNamespace.myuseercontrol>
<Button Name="uxRemove" Grid.Row="2" Grid.Column="5" VerticalAlignment="Center"    
        HorizontalAlignment="Right" Width="70" Content="Remove"
        Click="uxRemove_Click"/> 
</UserControl>

the main window is as follows : 主窗口如下

<Window>
<stackpanel>
    <local:MyUserControl x:Name="uxPanel1" />
</stackpanel>
</Window>

Requirements: 要求:

  1. I'd like to fire an event on the parent window when the event uxRemove_Click is fired at the usercontrl. 当在usercontrl触发事件uxRemove_Click时,我想在父窗口上触发一个事件。
  2. I'd like to add property Isenabled to the stackpanel "Usercontrol container" that will be based upon My usercontrol.IsEnabled property. 我想将属性Isenabled添加到stackpanel“Usercontrol容器”,该属性将基于My usercontrol.IsEnabled属性。

The MDI-style solution may be a bit overcomplicated for what you are doing. 对于您正在做的事情,MDI风格的解决方案可能有点过于复杂。

There is an example of raising events from child controls to a parent window here . 这里有一个从子控件向父窗口引发事件示例

This is the basic gist: 这是基本要点:

  • Declare a custom event type and a way to register handlers 声明自定义事件类型和注册处理程序的方法
  • Raise the event in the child control wherever you need to 在您需要的任何地方提升子控件中的事件
  • Have a handler on the parent window that listens for the event 在父窗口上有一个侦听事件的处理程序

This is a much simpler solution for you than implementing an MDI style WPF application. 与实现MDI样式的WPF应用程序相比,这是一个更简单的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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