実行時にデフォルトのウィンドウ スタイルを動的に更新して、実行時に FontSize と FontFamily を動的に変更できるようにしたいと考えています。リソース ディクショナリ内のスタイルは実行時に封印されており、変更できないことがわかったので、次の方法でスタイルを更新しました。
<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;
}
このアプローチにはいくつかの問題があり、なぜそうなのかについていくつか質問があります。
- スタイルが実行時に封印されるのはなぜですか? また、封印を解除する方法はありますか?
- 新しいスタイルを再度追加すると、すべてのウィンドウでこれが反映されないのはなぜですか? すべてのウィンドウに手動で適用する必要があるのはなぜですか?
- より良い方法はありますか?