1
我想在运行时动态更新默认的Window样式,以便在运行时动态更改FontSize和FontFamily。我发现,在你的资源字典样式在运行时密封的,不能改变的,所以我用更新的款式下面的方法:WPF在运行时更新样式
<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;
}
有几种这种方法存在问题,我有几个问题,为什么事情是这样的。
- 为什么样式在运行时是密封的,并且有一种使它们开启的方法?
- 当我重新添加新样式时,为什么我的所有窗口都没有拾取?为什么我必须手动将它应用到每个窗口?
- 有没有更好的方法?
+1至少手动的方法我到目前为止看到,欢呼! – kallotec 2014-05-02 02:05:56