Windows 窗体设计器自定义控件更改了锚点,该锚点在运行时正常工作,但在设计时无法正常工作

发布于 2024-10-16 09:28:34 字数 1012 浏览 0 评论 0原文

我的自定义控件在自定义标签上的锚点属性中设置了我想要的值。

将 Serialized 设置为 Visible 后,我将获得为运行时生成的代码,而我没有使用 Content 类型,但是设计器中的控件具有给定标签的锚点值(左和上),因此可以得到设计器中的正确行为需要手动(非)更改 Anchor 属性。

我真的不想写一个设计师来完成这项工作(以我的进度,我认为这不明智!),有没有更简单的方法?

public:
 [DesignerSerializationVisibility(DesignerSerializationVisibility::Visible)]
  virtual property System::Windows::Forms::AnchorStyles Anchor
   {
    System::Windows::Forms::AnchorStyles get() override
     {
      return static_cast<System::Windows::Forms::AnchorStyles
       ((System::Windows::Forms::AnchorStyles::Top
       | System::Windows::Forms::AnchorStyles::Left) 
       | System::Windows::Forms::AnchorStyles::Right);;
     }

    void set(System::Windows::Forms::AnchorStyles x) override
     {
      __super::Anchor = static_cast<System::Windows::Forms::AnchorStyles
       ((System::Windows::Forms::AnchorStyles::Top
       | System::Windows::Forms::AnchorStyles::Left) 
       | System::Windows::Forms::AnchorStyles::Right);

     }
   }

My custom control sets the values I want in the Properties for Anchor on my Custom Label.

With Serializable set to Visible I'm getting the code generated for run-time, which I didn't with type Content, but the control in the designer has the Anchor values a Label is given, (Left and Top), so to get the correct behaviour in the designer needs a manual (non-)change to the Anchor property.

I don't really want to write a designer to make this work (at my rate of progress I don't think that's wise!), is there an easier way?

public:
 [DesignerSerializationVisibility(DesignerSerializationVisibility::Visible)]
  virtual property System::Windows::Forms::AnchorStyles Anchor
   {
    System::Windows::Forms::AnchorStyles get() override
     {
      return static_cast<System::Windows::Forms::AnchorStyles
       ((System::Windows::Forms::AnchorStyles::Top
       | System::Windows::Forms::AnchorStyles::Left) 
       | System::Windows::Forms::AnchorStyles::Right);;
     }

    void set(System::Windows::Forms::AnchorStyles x) override
     {
      __super::Anchor = static_cast<System::Windows::Forms::AnchorStyles
       ((System::Windows::Forms::AnchorStyles::Top
       | System::Windows::Forms::AnchorStyles::Left) 
       | System::Windows::Forms::AnchorStyles::Right);

     }
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-10-23 09:28:35

您正在对属性值进行硬编码。因此,在构造函数中分配该值,使其不可浏览,这样它就不会显示在属性窗口中,并确保该值无法更改且不会被序列化。像这样:

ref class MyControl : Control {
public:
    MyControl() {
        __super::Anchor = AnchorStyles::Top | AnchorStyles::Left | AnchorStyles::Right;
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState::Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility::Hidden)]
    virtual property System::Windows::Forms::AnchorStyles Anchor {
        AnchorStyles get() override {
            return __super::Anchor;
        }
        void set(AnchorStyles) override {
            // do nothing
        }
    }
};

You are hardcoding the property value. So assign the value in the constructor, make it non-browsable so it won't show up in the properties window and ensure that the value cannot be changed and doesn't get serialized. Like this:

ref class MyControl : Control {
public:
    MyControl() {
        __super::Anchor = AnchorStyles::Top | AnchorStyles::Left | AnchorStyles::Right;
    }

    [Browsable(false), EditorBrowsable(EditorBrowsableState::Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility::Hidden)]
    virtual property System::Windows::Forms::AnchorStyles Anchor {
        AnchorStyles get() override {
            return __super::Anchor;
        }
        void set(AnchorStyles) override {
            // do nothing
        }
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文