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 ge开发者_StackOverflow中文版nerated 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);
}
}
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
}
}
};
精彩评论