WPF controls in ElementHost are not enabled correctly if ElementHost is created as disabled.

Article ID: 955753
Expand all | Collapse all
Source: Microsoft Support

RAPID PUBLISHING

RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.

Action



You develop a Winform application and add a ElementHost to host WPF controls in the designer or in the code.  The ElementHost's Enabled property is false or at least one of its ancestors' Enabled property is false.

You set the Enabled property above to true at runtime in the code.

Result

The WPF controls on the ElementHost look enabled, but do not respond to mouse or keyboard input.

Cause



When the ElementHost window is created HwndSource child window used by WPF is created with the same style.  So in this case, the ElementHost window and the HwndSource window are created disabled. 

When the Enabled state of a Winforms control is modified, Winforms traverses its child controls and enables/disables them accordingly.  However, the HwndSource child control is not enabled/disabled because it is not a .NET control.







So if the ElementHost window is created disabled, HwndSource child window will never get enabled.

Resolution



You can workaround this problem by always creating ElementHost window enabled.  However, this is not as simple as it sounds because its ancestors' Enabled property will also affect its enabled state.

Another workaround is through Win32 Interop.  You can derive a class from ElementHost as follows and use it instead of ElementHost:

    public class MyElementHost : ElementHost
    {
        protected override void OnEnabledChanged(EventArgs e)
        {
            SynchChildEnableState();

            base.OnEnabledChanged(e);
        }

        private void SynchChildEnableState()
        {
            IntPtr childHandle = GetWindow(Handle, GW_CHILD);
            if (childHandle != IntPtr.Zero)
            {
                EnableWindow(childHandle, Enabled);
            }
        }

        private const uint GW_CHILD = 5;

        [DllImport("user32.dll")]
        private extern static IntPtr GetWindow(IntPtr hWnd, uint uCmd);

        [DllImport("user32.dll")]
        private extern static bool EnableWindow(IntPtr hWnd, bool bEnable);
    }



DISCLAIMER

MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALS”) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.

TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.

Properties

Article ID: 955753 - Last Review: July 16, 2008 - Revision: 1.0
Keywords: 
kbnomt kbrapidpub KB955753

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com