I have a little problem with saving some properties of my Buttons. The Buttons are small and with a variety of colors. When i press one button, some specified colors are changing... and i want to save them for the next start up. The textbox values i can save them but this ...i can't.
Code:
public MainWindow()
{
InitializeComponent();
//blueColor.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
//this.Property = Properties.Settings.Default.userColor;
}
private void blueColor_Click(object sender, RoutedEventArgs e)
{
var bc = new BrushConverter();
Main.Background = (Brush)bc.ConvertFrom("#FF007CE4");
startButton.Foreground = (Brush)bc.ConvertFrom("#FF007CE4");
closeButton.Foreground = (Brush)bc.ConvertFrom("#FF007CE4");
Properties.Settings.Default.userColor = true;
Properties.Settings.Default.Save();
}
private void purpleColor_Click(object sender, RoutedEventArgs e)
{
var bc = new BrushConverter();
Main.Background = (Brush)bc.ConvertFrom("#FF8701B9");
startButton.Foreground = (Brush)bc.ConvertFrom("#FF8701B9");
closeButton.Foreground = (Brush)bc.ConvertFrom("#FF8701B9");
}
I think I need the last clicked Button to be saved because I have allot of colors and maybe the .RaiseEvent can help here.
This is how it looks like:
Those 3 little buttons:
- white
- blue
- red
are for changing the look of the program. At every start, the default is back.
You probably need to create items in the
Settings
tab of your project that store the information about the color. I would recommend storing the hex strings. Then, onMainForm_Load
retrieve those values.Make sure to also put the settings in the
User
scope, or else they will reset each time they close the application.You can store the color as a simple string and
TypeConverter
automatically converts it to typeBrush
. Below is an example.Binding default value from XAML:
Set value from code:
Note:
Setting - this is just the type ofString
.More information you can see here:
TypeConverters and XAML
Edit:
Below I'll show you an example, that I hope will help you.
So, go into the settings of the project:
Project -> Properties -> Parameters
. This opens a window of approximately:Here we have a property
ButtonColor
, defined in the settings. For example, I took theButton
, which changes the background, depending on the color of the pressed button.In order to property
Background
the synchronize with settings to do, so:The default background color of white. Now, to set the background color at the button, we change the parameter settings, like this:
To save changes to the settings, you need to call a method
Save()
:Now, the next time you start the program, the color will be the one that was set last.
Full example
XAML
Code behind
Output