簡體   English   中英

表單編輯器自動將子控件添加到WPF Integration ElementHost

[英]Forms editor automatically adding child control to WPF Integration ElementHost

我試圖在WinForms應用程序中使用WPF TextBox,同時在另一個程序集中完全封裝與WPF相關的細節,但是Forms編輯器並沒有簡化它。

也就是說,子訪問器總是被分配給一個新的System.Windows.Controls.TextBox,即使訪問者被另一個使用new的數據類型替換,並使用各種屬性進行扼殺,這些屬性應該導致它被忽略。 刪除條目會使表單編輯器重新生成它。

該值由控件本身分配,並且還會破壞我希望實現的封裝。

有沒有辦法阻止表單編輯器自動生成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; } }
    }

}

編輯:

到目前為止我找到的三種解決方法。 這里添加了因為這些都不符合答案。

  • 讓Forms編輯器負責TextBox的分配。

由於上述WPF細節和組件封裝的需要,這是不可接受的。

  • 不要讓Forms編輯器管理組件。

最煩人的。 使用Forms編輯器創建和管理組件會更受歡迎。

  • 將TextBox_SpellCheck(ElementHost)放在UserControl中。

只要表單編輯器不重新生成UserControl的設計器代碼(如果不是首先手動構建),則可以正常工作。 但是,這會增加一層不必要的控件嵌套。

更多的信息:

刪除TextBox_SpellCheck上的Designer屬性會使事情變得更糟,導致在設計器代碼中生成單獨的托管組件。

使用不同的類型既不會改善問題,也不會使問題變得更糟。

幾個例子:

  • ParentControlDesigner仍然生成子元素。
  • ScrollableControl仍然生成子元素。
  • DocumentDesigner拋出導致窗體編輯器不可用的異常。
  • System.ComponentModel.Design.ComponentDesigner將控件生成為間接可用的組件,例如通過Forms編輯器添加數據源或其他任何內容。

如果你這樣做的話,不確定你會找到一種方法。 ElementHost按設計使用Child,因為你使用它的確切原因 - 你在Windows窗體控件中托管一個WPF元素。 它總是會在設計器中生成您不喜歡的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM