简体   繁体   English

更改样式颜色后刷新WPF UI

[英]Refresh WPF UI after changing style color

I created a Template in a ResourceDictionary . 我在ResourceDictionary创建了一个模板。 I have created a window in order to change the ThemeColor. 我创建了一个窗口以更改ThemeColor。 This colors is binded to my ResourceDictionary . 此颜色绑定到我的ResourceDictionary

Here is my ResourceDictionary : 这是我的ResourceDictionary:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ns="clr-namespace:Phoenix_CRM" >


    <LinearGradientBrush x:Key="DegradeCouleurTheme" StartPoint="0,0" EndPoint="0,1" >
        <GradientStop Offset="0" Color="{x:Static ns:CParametres.ColorBegin}" />
        <GradientStop Offset="1" Color="{x:Static ns:CParametres.ColorEnd}"/>
    </LinearGradientBrush>
</ResourceDictionary>

I created a CParameters class in order to set the color parameters. 我创建了一个CParameters类以设置颜色参数。 The two color composing the gradient are save in a DataBase. 组成渐变的两种颜色保存在数据库中。 This class aims at Load/Save the color, and apply it. 此类旨在加载/保存颜色并应用颜色。

Here is my Class for changing the color : 这是我更改颜色的课程:

    public class CParametres : INotifyPropertyChanged
    {

        private  Color m_ThemeColorGradientBegin;
        public  Color ThemeColorGradientBegin
        {
            get { return m_ThemeColorGradientBegin; }
            set
            {
                m_ThemeColorGradientBegin = value;
                ColorBegin = m_ThemeColorGradientBegin;
                FirePropertyChangedEvent("ColorBegin");
            }
        }

        private  Color m_ThemeColorGradientEnd;
        public  Color ThemeColorGradientEnd
        {
            get { return m_ThemeColorGradientEnd; }
            set
            {
                m_ThemeColorGradientEnd = value;
                ColorEnd = m_ThemeColorGradientEnd;
                FirePropertyChangedEvent("ColorEnd");
            }
        }

        public static Color ColorBegin;
        public static Color ColorEnd;


        public CParametres()
        {
            ....
            ....
        }

        public void LoadGradientDefault()
        {
            ThemeColorGradientBegin   =   (Color)ColorConverter.ConvertFromString("#00b6e7");
            ThemeColorGradientEnd     =   (Color)ColorConverter.ConvertFromString("#0086d6");
        }

        public void LoadParams()
        {
            if (ReadParamFromDB() == true)
            {
                setThemeGradient(m_ThemeColorGradientBegin, m_ThemeColorGradientEnd);
            }
        }

        public void setThemeGradient(Color ColorBegin, Color ColorEnd)
        {
            this.ThemeColorGradientBegin = ColorBegin;
            this.ThemeColorGradientEnd = ColorEnd;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void FirePropertyChangedEvent(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

When I change the color, I need to quit and re-run my application in order to see the changes. 更改颜色时,需要退出并重新运行我的应用程序才能看到更改。 Anyone know of forcing to apply directly the new colors please? 有人知道请强迫直接应用新颜色吗?

Anyone could explain me what is the tips in order to refresh the UI please? 任何人都可以解释一下刷新UI的提示是什么?

You need to use DynamicResource for all resource colors & styles instead of StaticResource. 您需要为所有资源颜色和样式使用DynamicResource,而不是StaticResource。

Using DynamicResource, all your changes styles will be reflected in UI. 使用DynamicResource,所有更改样式将反映在UI中。

Update: 更新:

When ever you use Brush, you need to give the Brush in this way 每当您使用画笔时,都需要以这种方式使用画笔

<Setter Property="Foreground">
    <Setter.Value>
        <SolidColorBrush Color="{DynamicResource AccentColor}" />
    </Setter.Value>
</Setter>

You need to implement INotifyPropertyChanged interface for property change events to occur. 您需要实现INotifyPropertyChanged接口,以便发生属性更改事件。 Unfortunately that means you need an instance to operate on rather than a static. 不幸的是,这意味着您需要一个实例而不是静态实例来进行操作。

If you create a static Property that returns a singleton instance you can do the following 如果创建返回单例实例的静态属性,则可以执行以下操作

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ns="clr-namespace:Phoenix_CRM" >


    <LinearGradientColor x:Key="DegradeCouleurTheme" StartPoint="0,0" EndPoint="0,1" >
        <GradientStop Offset="0" Color="{Binding ColorBegin, Source={x:Static ns:CParametres.Instance}}" />
        <GradientStop Offset="1" Color="{Binding ColorEnd, Source={x:Static ns:CParametres.Instance}}" />
    </LinearGradientColor>
</ResourceDictionary>

Example of singleton 单例的例子

public class CParameters : INotifyPropertyChanged {

  public event PropertyChangedEventHandler PropertyChanged;

  private void OnPropertyChanged(string propertyName) {
    var handle = PropertyChanged;
    if (handle != null) handle(this, new PropertyChangedEventArgs(propertyName));
  }

  private static readonly CParameters instance = new CParameters();

  public static CParameters Instance {
     get { return instance; }
  }

  private Color colorBegin;

  private Color colorEnd;

  public Color ColorBegin {
    get { return colorBegin; }
    set {
      if (value != colorBegin) {
        colorBegin=value;
        OnPropertyChanged("ColorBegin");
      }
    }
  }


  public Color ColorEnd {
    get { return colorEnd; }
    set { 
     if (value != colorEnd) {
       colorEnd = value;
       OnPropertyChanged("ColorEnd");
     }
   }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM