Windows 窗体设计器自定义控件更改了锚点,该锚点在运行时正常工作,但在设计时无法正常工作
我的自定义控件在自定义标签上的锚点属性中设置了我想要的值。
将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在对属性值进行硬编码。因此,在构造函数中分配该值,使其不可浏览,这样它就不会显示在属性窗口中,并确保该值无法更改且不会被序列化。像这样:
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: