c# - How to create custom properties in user control? -
i have user command 2 fields:
public string offtext { set; get; } public string ontext { set; get; } after added command in form , fill offtext , ontext properties. in control's constructor have:
public faketoggleswitch() { initializecomponent(); if (state) { checkedit.text = ontext; } else { checkedit.text = offtext; } } and in debug mode see ontext , offtext null. whats can wrong here? have create fields?
those not fields, auto-properties.
if utilize auto-property , default value should different 0 (value type) or null (reference type), can set in constructor
public string offtext { set; get; } public string ontext { set; get; } public constructor() { // init offtext = "..."; ontext = "..."; } otherwise may decide utilize normal properties
private string _offtext = "..."; // default value public string offtext { { homecoming _offtext; } set { _offtext = value; } } if utilize wpf, typically usercontrol properties there have dependency properties (to back upwards binding). creating dependency property done using code snippets. type
propdp tabtab
to get
public int myproperty { { homecoming (int)getvalue(mypropertyproperty); } set { setvalue(mypropertyproperty, value); } } // using dependencyproperty backing store myproperty. enables animation, styling, binding, etc... public static readonly dependencyproperty mypropertyproperty = dependencyproperty.register("myproperty", typeof(int), typeof(ownerclass), new propertymetadata(0)); c# user-controls
No comments:
Post a Comment