This one stumped me for a while. There are a lot of blog posts out there on how to change the font family of a particular style if you know the key, or how to do it for all textblocks in the application. The problem I was facing was that I used a lot of Labels and other controls that did not pick up these changes. Not only that, but I found no way of editing my default styles at runtime. After trying several approaches, I came up with this solution. I don’t know if it will work for everyone, but in my case, it is working beautifully. I hope it helps.
private void applyFontFamilySettings()
{
foreach (DictionaryEntry entry in Application.Current.Resources)
{
if (entry.Value is Style)
{
Style originalStyle = (Style)entry.Value;
swapStyles(originalStyle, entry.Key, originalStyle.TargetType);
}
}
}
private void swapStyles(Style s, object styleKey, Type t)
{
Style newStyle = new System.Windows.Style()
{
TargetType = t
};
foreach (Setter st in s.Setters)
{
newStyle.Setters.Add(st);
}
newStyle.Setters.Add(new Setter(FontFamilyProperty, UiSettings.FontFamily));
Application.Current.Resources[styleKey] = newStyle;
}
No comments:
Post a Comment