更改样式颜色后刷新WPF UI

时间:2013-04-11 15:06:54

标签: c# wpf xaml templates user-interface

我在ResourceDictionary中创建了一个模板。 我创建了一个窗口以更改ThemeColor。 这些颜色绑定到我的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>

我创建了一个CParameters类来设置颜色参数。组成渐变的两种颜色保存在DataBase中。本课程旨在加载/保存颜色并应用它。

以下是我更改颜色的类:

    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));
            }
        }

    }

当我更改颜色时,我需要退出并重新运行我的应用程序才能看到更改。有人知道强迫直接申请新颜色吗?

任何人都可以向我解释一下,为了刷新用户界面有什么建议吗?

2 个答案:

答案 0 :(得分:1)

您需要将DynamicResource用于所有资源颜色和&amp;样式而不是StaticResource。

使用DynamicResource,您的所有更改样式都将反映在UI中。

更新

当你使用Brush时,你需要以这种方式提供画笔

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

答案 1 :(得分:-1)

您需要实现INotifyPropertyChanged接口才能发生属性更改事件。不幸的是,这意味着您需要一个实例来操作而不是静态。

如果创建一个返回单例实例的静态属性,则可以执行以下操作

<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>

单身人士的例子

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");
     }
   }
  }
}