Set AppStylist Font at Runtime

Jason Beres [Infragistics] / Saturday, February 28, 2009

If you do not want hard code the font name in an AppStylist ISL file and instead determine it at runtime and set this value in code. 

private void ultraFontNameEditor1_ValueChanged(object sender, EventArgs e)
{
    ApplicationStyleLibrary styleLibrary = new ApplicationStyleLibrary();
    styleLibrary.LoadFromStyleManager();

    StyleSetSettings styleSet = null;
    if (styleLibrary.StyleSets.Count > 0 && styleLibrary.DefaultStyleSet != null)
        styleSet = styleLibrary.StyleSets[styleLibrary.DefaultStyleSet];

    if (styleSet == null)
    {
        styleSet = styleLibrary.StyleSets.Add("Default");
        styleLibrary.DefaultStyleSet = styleSet.Key;
    }

    StyleSettings baseRole = styleSet.RoleStyles.GetStyle("Base", true);
    StateSettings normalState = (baseRole.States.Exists(RoleState.Normal))
        ? baseRole.States[RoleState.Normal]
        : baseRole.States.Add(RoleState.Normal);

    normalState.Appearance.FontData.Name = this.ultraFontNameEditor1.Text;

    styleLibrary.UpdateStyleManager();
}