上一篇文章讲到了如果获得Windows10主题色
但是,当窗口改变其颜色时,背景颜色不会自动更新。不幸的是,SystemParameters没有提供WindowGlassBrushKey或WindowGlassColorKey属性来使用DynamicResource作为resourcekey,因此获取更改通知需要后面的代码来处理StaticPropertyChanged事件。
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
SystemParameters.StaticPropertyChanged += this.SystemParameters_StaticPropertyChanged;
// Call this if you haven't set Background in XAML.
this.SetBackgroundColor();
}
protected override void OnClosed(EventArgs e)
{
SystemParameters.StaticPropertyChanged -= this.SystemParameters_StaticPropertyChanged;
base.OnClosed(e);
}
private void SetBackgroundColor()
{
this.Background = SystemParameters.WindowGlassBrush;
}
private void SystemParameters_StaticPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "WindowGlassBrush")
{
this.SetBackgroundColor();
}
}
}
效果如下图:
这样问题就解决了,但是,怎么像动态binding来使用SystemParameters.WindowGlassBrush的值,这个问题欢迎大神提供解决思路。
原文参考地址