Question

I'm attempting to use the WPF TextBox in a WinForms application while completely encapsulating WPF-related details within another assembly, but the Forms editor is not making it simple.

Namely, the Child accessor is always being assigned to a new System.Windows.Controls.TextBox, even if the accessor is replaced with another datatype using new and smothered with various attributes that should cause it to be ignored. Removing the entry causes it to be regenerated by the forms editor.

The value is assigned by the control itself, and additionally ruins the encapsulation I hope to achieve.

Is there a way to prevent the Forms editor from automatically generating the Child?

    // 
    // textBox_SpellCheck1
    // 
    this.textBox_SpellCheck1.Location = new System.Drawing.Point(12, 12);
    this.textBox_SpellCheck1.Name = "textBox_SpellCheck1";
    this.textBox_SpellCheck1.Size = new System.Drawing.Size(200, 100);
    this.textBox_SpellCheck1.TabIndex = 0;
    this.textBox_SpellCheck1.Text = "textBox_SpellCheck1";
    //The Forms editor should not be generating the following line:
    this.textBox_SpellCheck1.Child = new System.Windows.Controls.TextBox();

Example which reproduces the issue, when it is placed within a form:

using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Controls; //reference PresentationCore, PresentationFramework
using System.Windows.Forms.Integration; //reference WindowsFormsIntegration
using System.Windows.Forms.Design;

namespace wtf
{
    [Designer(typeof(ControlDesigner))] //reference System.Design
    public class TextBox_SpellCheck : ElementHost
    {
        private System.Windows.Controls.TextBox textbox;

        public TextBox_SpellCheck()
        {
            textbox = new System.Windows.Controls.TextBox();
            base.Child = textbox;
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DefaultValue(0)]
        public new int Child { set { } get { return 0; } }
    }

}

Edit:

The three workarounds I've found thus far. Added here because none of these qualify as an answer.

  • Let the Forms editor take care of allocation of the TextBox.

Unacceptable, due to the above desire of the encapsulation of WPF details and assemblies.

  • Don't let the Forms editor manage the component at all.

Annoying at best. It would be much preferred to create and manage the components using the Forms editor.

  • Place the TextBox_SpellCheck (ElementHost) within a UserControl.

Works as long as the Forms editor doesn't regenerate the designer code for the UserControl, if not built manually in the first place. However, this adds a layer of unnecessary control nesting.

Further Information:

Removal of the Designer attribute on the TextBox_SpellCheck makes matters worse, causing a separate hosted component to be generated within the designer code.

Using different types either doesn't improve matters, or makes them worse.

A few examples:

  • ParentControlDesigner still generates the child element.
  • ScrollableControl still generates the child element.
  • DocumentDesigner throws exceptions that cause the Forms editor to be unusable.
  • System.ComponentModel.Design.ComponentDesigner generates the control as an indirectly usable component, such as adding data sources or whatever via the Forms editor.
Was it helpful?

Solution

Not sure you'll find a way if you're set on doing it that way. ElementHost by design uses Child for the exact reason you're using it for - you're hosting an WPF element inside of a Windows Form control. Its always going to generate the code you are adverse to in the designer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top