您可以创建一个实现 INotifyPropertyChanged
接口并拥有您的 FontSize
属性的类:
public class UserSettings : INotifyPropertyChanged
{
private double _fontSize = 20;
public double FontSize
{
get { return _fontSize; }
set { _fontSize = value; OnPropertyChanged(); }
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
现在,在应用程序启动期间创建此类的一个实例,并将其存储为 Resource
:
Application.Current.Resources["UserSettings"] = new UserSettings()
现在,使用数据绑定来绑定所需 TextBlock
的 FontSize
属性:
<TextBlock FontSize="{Binding FontSize, Source={StaticResource UserSettings}}"
Text="Something" />
在应用程序设置中,您可以像这样修改值:
( ( UserSettings )Application.Current.Resources["UserSettings"] ).FontSize = newFontSize
但请记住,这样做会带来性能损失,最好静态设置并要求用户重新启动应用程序。更好的做法是使用系统范围内的字体大小设置,因为如果用户喜欢更大的字体,她可能希望在整个应用中都使用更大的字体。