您当前的位置: 首页 >  windows

君子居易

暂无认证

  • 0浏览

    0关注

    210博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

(二)在WPF中获得Windows 10或8的主题色

君子居易 发布时间:2019-05-07 10:49:38 ,浏览量:0

 上一篇文章讲到了如果获得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的值,这个问题欢迎大神提供解决思路。

原文参考地址

 

关注
打赏
1660814979
查看更多评论
立即登录/注册

微信扫码登录

0.0358s