主要控制关闭孩子 [英] Main control to close child

查看:121
本文介绍了主要控制关闭孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MainControl ,其中包含一个 ChildControl ChildControl 具有隐藏自身的隐藏按钮。
当隐藏时,我希望 MainControl 挂起事件并处理它。

I have one MainControl that contains a ChildControl. The ChildControl has a hide button that would hide itself. When hidden I expect the MainControl to hook the event and dispose it.


  • MainControl


    • ChildControl>隐藏按钮

    无法弄清楚我应该如何挂钩。

    Can't figure out how I should hook those.

    任何提示?谢谢!

    推荐答案

    您可以创建一个事件,通知主控件子控件被隐藏,并在您的主控制,处理事件,您可以处理您的控制。

    You can create an event that will notify the main control that the child control is hidden, and in your main control, handling the event, you can dispose of your control.

    以下是一个小型示例代码,介绍如何为隐藏的操作创建事件。 p>

    Below is a small sample code of how you can go about creating your event for the hidden action.

        class MainControl
        {
            ChildControl childControl;
    
            public MainControl()
            {
                childControl = new ChildControl();
                childControl.VisibilityChanged += childControl_VisibilityChanged;
            }
    
            void childControl_VisibilityChanged(object sender, HiddenEvent e)
            {
                if (e.isHidden)
                {
                    //close control here
                }
            }
        }
    
        public class HiddenEvent : EventArgs
        {
            public HiddenEvent(bool propertyValue)
            {
                this.isHidden = propertyValue;
            }
    
            public bool isHidden { get; set; }
        }
        public class ChildControl
        {
            public event EventHandler<HiddenEvent> VisibilityChanged;
    
            public ChildControl()
            {
    
            }
    
            private bool _isHidden;
            public bool Control
            {
                get
                {
                    return _isHidden;
                }
                set
                {
                    _isHidden = value;
                    Hidden_Handler(value);
                }
            }
    
            private void Hidden_Handler(bool isHidden)
            {
                var handler = VisibilityChanged;
                if (handler != null)
                    VisibilityChanged(this, new HiddenEvent(isHidden));
            }
        }
    

    这篇关于主要控制关闭孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆