private Style _headerFontStyle;
[Bindable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
NotifyParentProperty(true),
Category("Appearance"),
Description("The font for the header."),
Localizable(true)]
public FontInfo HeaderFont
{
get
{
if (_headerFontStyle == null)
{
_headerFontStyle = new Style();
}
return _headerFontStyle.Font;
}
set
{
_headerFontStyle.Font.CopyFrom(value);
}
}
So is that all there is to it? Well, not exactly. Unfortunately, FontInfo is not serializable, nor does it implement IStateManager making it difficult for us to track it's state if any changes are programmatically made to the structure.
The good news is that in the code above we're using a Style object to get at the underlying FontInfo. Since Style implements IStateManager we can override the following methods to make sure that the FontInfo property is always kept in ViewState across post backs.
protected override object SaveViewState()
{
object[] state = new object[2];
state[0] = base.SaveViewState();
state[1] = ((IStateManager)_headerFontStyle).SaveViewState();
return state;
}
protected override void LoadViewState(object savedState)
{
object[] state = (object[])savedState;
base.LoadViewState(state[0]);
((IStateManager)_headerFontStyle).LoadViewState(state[1]);
}
protected override void TrackViewState()
{
base.TrackViewState();
if (_headerFontStyle != null)
{
((IStateManager)_headerFontStyle).TrackViewState();
}
}
No comments:
Post a Comment