【发布时间】:2011-01-18 17:05:05
【问题描述】:
我想在运行时动态更新默认的 Window 样式,以便在运行时动态更改 FontSize 和 FontFamily。我发现你的资源字典中的Styles在运行时是密封的,无法更改,所以我使用了以下方法来更新样式:
<Style TargetType="{x:Type Window}">
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="12pt"/>
</Style>
使用以下代码:
Style newStyle = (Make a copy of the old style but with the FontSize and FontFamily changed)
// Remove and re-add the style to the ResourceDictionary.
this.Resources.Remove(typeof(Window));
this.Resources.Add(typeof(Window), newStyle);
// The style does not update unless you set it on each window.
foreach (Window window in Application.Current.Windows)
{
window.Style = newStyle;
}
这种方法有几个问题,我有几个问题是为什么事情会这样。
- 为什么样式在运行时被密封,有没有办法让它们解封?
- 当我重新添加新样式时,为什么我的所有窗口都没有选择它?为什么我必须手动将其应用到每个窗口?
- 有没有更好的办法?
【问题讨论】:
标签: c# wpf styles font-size resourcedictionary