问题描述
I want to create a custom control property that can only be set at design time is it possible ?
Yes... In your class, create a private field to identify if your property has been initially "set" or not. Then have your standard property getter and setter. However, in the setter, use the license manager to detect which mode you are operating under ... designtime vs runtime. Then check. Always allow if in Design-Time, OR if the field has never been set yet. Once in, then set the flag as being set. This will be needed since during the form designer's instantiation of the controls, it has to be set at least ONCE from the .Designer.cs code, but after that, ignore any attempts to change it -- via setting the flag.
private Boolean IsCreated = false;
private String myVar1;
public String MyVar1
{
get { return myVar1; }
set {
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime
|| !IsCreated)
{
myVar1 = value;
IsCreated = true;
}
}
Now, you could still allow a change, but you would have to do it via a custom method created in your class since the "IsCreated" flag is PRIVATE and not PROTECTED for child-inheritance to muck around with. You would have to clear the flag, then reset to the new string (or whatever ) value your property was to hold
这篇关于是否有可能创建一个自定义的控件属性仅在设计时设定的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!