0

I am using .NET 4.5 with winforms. I use ElementHost in order to use XAML within my application, where the only XAML portion is the Ribbon. My application has one parent winform, hosting multiple child winforms.

When I click on any child forms within the application, the forms focus as expected (the GotFocus event handler). However, when I click on anything that is a dropdown on the ribbon, the child forms no longer trigger the GotFocus handler even though I can still drag the forms with my mouse. Also, when I click on regular buttons that are not dropdown within the Ribbon, the child form will activate the GotFocus handler as expected.

Here is example dropdown code from the ribbon. If I click on the main button that triggers the dropdown, I can no longer trigger the GotFocus handler on any child forms.

<RibbonMenuButton LargeImageSource="" >
    <RibbonMenuItem ImageSource = "" />
    <RibbonMenuItem ImageSource = "" />
    <RibbonMenuItem ImageSource = "" />
</RibbonMenuButton>

Did I discover a bug perhaps?

Mike
  • 235
  • 3
  • 15
  • The problem goes away when there are two or more forms open. When a user clicks on another form, it reactivates the focus of the original form when clicking back onto the form. However if only one form is open, the form seems to permanently lose its focus. – Mike Jul 01 '15 at 15:45
  • Not ideal, but a workaround is to add Focusable="False" like so: . I still find no decent way to divert focus back onto the target child form, at least without any unusual hack. – Mike Jul 01 '15 at 18:11

1 Answers1

0

Seems OK to me. Maybe you can modify this example to recreate your problem:

        Form fff = new Form();
        ElementHost eh = new ElementHost() { Dock = DockStyle.Top };
        var dp = new System.Windows.Controls.DockPanel();
        eh.BackColor = Color.LightYellow;
        dp.Background = System.Windows.Media.Brushes.LightSkyBlue;
        eh.Child = dp;
        var combo = new System.Windows.Controls.ComboBox();
        combo.Items.Add("Value1");
        combo.Items.Add("Value2");
        combo.Items.Add("Value3");
        dp.Children.Add(combo); //new System.Windows.Controls.Button { Content = "Button Text" });
        fff.Controls.Add(eh);
        fff.IsMdiContainer = true;
        var fc1 = new Form { TopLevel = false, Visible = true, Size = new Size(300,300), Location = new Point(100,100) };
        var fc2 = new Form { TopLevel = false, Visible = true, Size = new Size(300,300), Location = new Point(100,100) };

        fc1.MdiParent = fff;
        fc2.MdiParent = fff;

        fc1.GotFocus += delegate {
            fc1.Text = "Got focus1";
        };
        fc2.GotFocus += delegate {
            fc2.Text = "got focus2";
        };
        fc1.LostFocus += delegate {
            fc1.Text = "Lost focus1";
        };
        fc2.LostFocus += delegate {
            fc2.Text = "Lost focus2";
        };


        Application.Run(fff);
Loathing
  • 5,109
  • 3
  • 24
  • 35