Live Chat Icon For mobile
Live Chat Icon

How do I prevent the default values of my Localized properties form being set?

Platform: WinForms| Category: Serialization

It is normal to have Properties in your Control/Component whose default values are

inherited from some other Control/Component.

In such cases you will normally prevent the designer from storing the property’s

value in code (using either DefaultValue attribute or the ShouldSerializeXXX

pattern). However, if that property is Localizable and Localization is turned on,

then the property’s value will be forced to be stored in the resource. This will

break your property-inheritance logic.

For example:


[
Localizable(true)
...
]
public Font MyControlButtonFont
{
	get
	{
		if(this.buttonFont == null)
			return this.Font;
		else
			return this.buttonFont;
	}
	set
	{
		this.buttonFont = value;
	}
}

private bool ShouldSerializeMyControlButtonFont()
{
	if(this.MyControlButtonFont == this.Font)
		return false;
	else
		return true;
}

In the above case the MyControlButtonFont inherits its value from the Font

property, if its value is not set. And you use null to determine whether the value

is set or not.

But when Localization is ON, the property gets SET and you lose the inheritance

logic.

You can avoid this by specifying an AmbientValue attribute for your property, as

follows:


[
Localizable(true),
AmbientValue(null)
...
]
public Font MyControlButtonFont

This will use the AmbientValue as the value to persist when there is default-value

in your property. This will prevent your property from getting SET unnecessarily.

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.