【问题标题】:C# All WPF controls using a common event in Windows FormC# 所有 WPF 控件在 Windows 窗体中使用公共事件
【发布时间】:2018-01-17 14:06:59
【问题描述】:

我已经创建了一个这样的自定义 WPF 控件:

我将此 WPF 控件放置在我的 Form1(Windows 窗体)中并为其编写了一个事件 MouseEnter。我在这里期望的是在 WPF 控件中获取按钮的名称。我在 Form1 中只使用了 1 个 WPF 控件。但我在 Form1 中有 24 个 WPF 控件。我使用 foreach 循环遍历所有 ElementHost (WPF 控件)并将每个 ElementHost 的子项转换为 WPF 控件。我在将 ElementHost 转换为 WPF 控件时遇到问题。

这是我的代码:

   public Form1()
        {
            InitializeComponent();
            foreach (ElementHost c in this.Controls)
            {        

                     TP1WPFControls.TP1CustomButton bt = c.Child as TP1CustomButton;

                    bt.bt.MouseEnter += Bt_MouseEnter;    
                    bt.bt.MouseLeave += Bt_MouseLeave;
            }

            //TP1WPFControls.TP1CustomButton btnCustom = elementHost1.Child as TP1CustomButton;
            //btnCustom.bt.Click += Bt_Click;
            //btnCustom.bt.MouseEnter += Bt_MouseEnter; ;
            //btnCustom.bt.MouseLeave += Bt_MouseLeave; ;

        }

这是我在 Form1 中显示 WPF 控件的图片:

希望大家可以给我一个解决方案。

谢谢!

【问题讨论】:

    标签: c# winforms wpf-controls


    【解决方案1】:

    你需要遍历表单控件,但不是所有的都是元素宿主:

    每个 Windows 窗体控件都基于 System.Windows.Forms.Control,包括 ElementHost。因此,在循环中,您尝试对此进行强制转换。如果它不为空,则您知道它是一个 ElementHost,那么您可以在 ElementHost 上执行您的代码。

    foreach (System.Windows.Forms.Control e in this.Controls)
    {        
    
        ElementHost c= e as ElementHost;
        if ( c != null ) 
        {
            TP1WPFControls.TP1CustomButton bt = c.Child as TP1CustomButton;
            if ( bt != null )
            {
                    // Add Events (I'd assume it would just be bt.MouseEnter but I don't know what your control looks like)
                    bt.bt.MouseEnter += Bt_MouseEnter;    
                    bt.bt.MouseLeave += Bt_MouseLeave;
            }
        }
    }
    

    【讨论】:

    • 感谢您的帮助。我现在没有错误,但只有第一个 WPF 控件可以使用这些事件。第二个没有效果。
    • 您的元素宿主中是否包含多个按钮...该代码会有所不同
    • 在 WPF 控件中,我有一个标签(左侧)和一个按钮(右侧)调用“bt”
    • 元素宿主中有多少个 wpf 控件......它是 1 个吗?......它是多个元素宿主,每个元素中有 1 个......还是 1 个元素宿主中有多个按钮...这会改变代码
    • 是的foreach循环只获取Form1中的第一个WPF控件,1个元素宿主有1个标签和1个按钮,就像我在主题顶部发送的图片一样
    【解决方案2】:

    非常感谢@Ctznkane525。我试图将 MouseLeave 和 MouseEnter 事件中的发送者转换为 WPF 自定义控件中每个控件的类型,它已经奏效了!因为我的 TPCustomButton 只是一个 WPF 控件,所以我必须为上面的每个事件在其中转换每个控件。

    我已编辑我的答案以获得完整信息:

     public Form1()
            {
                InitializeComponent();
                foreach (Control c in this.Controls)
                {
                    ElementHost e = c as ElementHost;
                    if (e != null)
                    {
    
                        TP1WPFControls.TP1CustomButton btWPF = e.Child as TP1CustomButton;
                        if (btWPF != null)
                        {
    
                            //bt.bt.MouseLeave += Bt_MouseLeave;
                            //bt.lbl.MouseLeave += Lbl_MouseLeave;
                            btWPF.MouseLeave += Bt_MouseLeave;
                        }
                    }
                }
    
    
            }
       private void Bt_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
            {
                TP1CustomButton btCustom = sender as TP1CustomButton;
    
    
                brushcolor = new brushColor(mediaColor.FromRgb(221, 247, 190));
                btCustom.bt.Background = brushcolor;
                btCustom.lbl.Background = brushcolor;
    
            }
    

    【讨论】:

    • 谢谢我标记了你的答案。你的解决方案给了我很多想法来制作更多的 WPF 自定义控件。
    猜你喜欢
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多