Reputation: 5729
I created a Template in order to set a Theme for my Application. In this Template, i created a Gradient style (composed of two colors) :
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<LinearGradientBrush x:Key="DegradeCouleurTheme" StartPoint="0,0" EndPoint="0,1">
<!--<GradientStop Offset="0" Color="#00b6e7"/>
<GradientStop Offset="1" Color="#0086d6"/>-->
<GradientStop Offset="0" Color="{Binding Path=(m_ThemeColorGradientBegin)}"/>
<GradientStop Offset="1" Color="{Binding Path=(m_ThemeColorGradientEnd)}"/>
</LinearGradientBrush>
</ResourceDictionary>
I would like to be able to change the two colors, so i have created a Window in order to set the colors. These colors are stored in a class "CParametres.cs" .The two colors are nextly stored in the DataBase.
Here is my CParameters Class : (I cleaned the class for a better view )
namespace Phoenix_CRM
{
public class CParametres : INotifyPropertyChanged
{
private Color m_ThemeColorGradientBegin;
public Color ThemeColorGradientBegin
{
get { return m_ThemeColorGradientBegin; }
set
{
m_ThemeColorGradientBegin = value;
FirePropertyChangedEvent("ThemeColorGradientBegin");
}
}
private Color m_ThemeColorGradientEnd;
public Color ThemeColorGradientEnd
{
get { return m_ThemeColorGradientEnd; }
set
{
m_ThemeColorGradientEnd = value;
FirePropertyChangedEvent("ThemeColorGradientEnd");
}
}
public CParametres(r)
{
......
}
public void LoadParams()
{
if (ReadParamFromDB() == true)
{
setThemeGradient(m_ThemeColorGradientBegin, m_ThemeColorGradientEnd);
}
}
public void setThemeGradient(Color ColorBegin, Color ColorEnd)
{
ThemeColorGradientBegin = ColorBegin;
ThemeColorGradientEnd = ColorEnd;
}
public event PropertyChangedEventHandler PropertyChanged;
private void FirePropertyChangedEvent(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
In my App.xaml.cs, i created a object "CParametres" and i load the colors from the database in this object :
In my App.xamls.cs
obj_Parametres = new CParametres()
obj_Parametres .LoadParams()
After this LoadParams(), the two colors are stored in the :
m_ThemeColorGradientBegin and m_ThemeColorGradientEnd objects.
I'm trying to do the binding for this two colors to my Template, and, when i run my application, i don't have any crash, but the colors aren't apply.
I debug my application, and the LoadParams function is OK, my m_ThemeColorGradientBegin and m_ThemeColorGradientEnd objects contains colors.
Anyone could explain my why my binding is not ok ?
Thanks a lot :)
Best regards,
Nixeus
--EDIT-- After MoHaKa tips :
Template :
<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="#00b6e7"/>
<GradientStop Offset="1" Color="#0086d6"/>-->
<GradientStop Offset="0" Color="{x:Static ns:CParametres.ThemeColorGradientBegin}"/>
<GradientStop Offset="1" Color="{x:Static ns:CParametres.ThemeColorGradientBegin}"/>
</LinearGradientBrush>
</ResourceDictionary>
My CParametres.CS :
private static Color m_ThemeColorGradientBegin;
public static Color ThemeColorGradientBegin
{
get { return m_ThemeColorGradientBegin; }
set
{
m_ThemeColorGradientBegin = value;
FirePropertyChangedEvent("ThemeColorGradientBegin");
}
}
With setting static to ThemeColorGradientBegin and m_ThemeColorGradientBegin i have a compilator error on the FirePropertyChangedEvent() : Error MC3011 : Unable to find statical member ThemeColorGradientBegin on CParametres type.
Have you an idea please ?
Thanks :)
Upvotes: 1
Views: 203
Reputation: 1906
THIS code will help you:
define this statement in your window tag.
xmlns:ns="clr-namespace:Phoenix_CRM"
now using binding get the colors:
<LinearGradientBrush x:Key="DegradeCouleurTheme" StartPoint="0,0" EndPoint="0,1">
<!--<GradientStop Offset="0" Color="#00b6e7"/>
<GradientStop Offset="1" Color="#0086d6"/>-->
<GradientStop Offset="0" Color="{x:Static ns:CParametres.ThemeColorGradientBegin}"/>
<GradientStop Offset="1" Color="{x:Static ns:CParametres.ThemeColorGradientEnd}"/>
</LinearGradientBrush>
don't forgot to change your color properties to static
Upvotes: 2