表单编辑器自动将子控件添加到WPF Integration ElementHost。

3
我试图在WinForms应用程序中使用WPF TextBox,并完全将与WPF相关的细节封装在另一个程序集中,但是表单编辑器没有简化它。显然,即使使用new替换访问器并添加各种属性以使其被忽略,Child访问器仍然总是被分配给新的System.Windows.Controls.TextBox。如果删除该条目,则会由表单编辑器重新生成。该值由控件本身分配,并且会破坏我希望实现的封装性。是否有一种方法可以防止表单编辑器自动生成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();

以下是一个在表单中引起问题的示例:

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; } }
    }

}
编辑:

到目前为止,我发现了三个解决方法。添加在此处,因为这些都不能算作答案。

  • 让表单设计器来处理 TextBox 的分配。

这是不可接受的,因为上述需求要求 WPF 详细信息和程序集封装。

  • 根本不让表单设计器管理组件。

最多只是有点烦人。最好还是使用表单设计器来创建和管理组件。

  • 将 TextBox_SpellCheck (ElementHost) 放在 UserControl 中。

只要表单设计器不重新生成 UserControl 的设计器代码(如果首先是手动构建的),就可以正常工作。然而,这会增加一层不必要的控件嵌套。

更多信息:

删除 TextBox_SpellCheck 上的 Designer 特性会使问题变得更糟,并导致在设计器代码中生成一个单独的托管组件。

使用不同类型不会改善情况,甚至使它们变得更糟。

以下是一些例子:

  • ParentControlDesigner 仍会生成子元素。
  • ScrollableControl 仍会生成子元素。
  • DocumentDesigner 抛出异常,导致表单设计器无法使用。
  • System.ComponentModel.Design.ComponentDesigner 生成控件作为间接可用的组件,例如通过表单设计器添加数据源或其他内容。
1个回答

2

如果你坚持这种方式,不确定是否能找到解决方法。 ElementHost 设计上使用 Child 是因为你正在将 WPF 元素托管在 Windows Form 控件中,这正是你使用它的原因。无论如何,在设计器中它总是会生成你所反感的代码。


在未发现任何相反的信息时,关于代码生成... - unsigned long double

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接