1

I am designing a simple application in C# and WPF with multiple buttons that change their color when clicked.

Each individual button changes colors independently to red (1st click) and then to green (on the 2nd click).

I am looking for a way to make this changes persistent between application runs. In other words, if 1 button has been set to green and 1 button to red, I would like them to keep their color (unless i change it) regardless of how many times I open and close the application. Examples of code that i can use are more than welcomed. Any help or example of code is highly appreciated. Thank you!

This is the WPF:

    <Button x:Name="btn0" Focusable="False"  Margin="277,100,173,148" Click="btn0_Click" Content="access"/>
    <Button x:Name="btn1" Focusable="False"  Margin="189,100,253,148" Click="btn1_Click" Content="access"/>

And here is the code (i know it's a bit messy):

    namespace WpfApp7
    {
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }       
    private bool _IsOn;
    public bool IsOn
    {
        get
        {
            return _IsOn;
        }
        set
        {
            _IsOn = value;
            btn0.Background = _IsOn ? Brushes.Green : Brushes.Red;
        }
    }
    private void btn0_Click(object sender, RoutedEventArgs e)
    {
        IsOn = !IsOn;
    }
    private bool _IsOn1;
    public bool IsOn1
    {
        get
        {
            return _IsOn1;
        }
        set
        {
            _IsOn1 = value;
            btn1.Background = _IsOn1 ? Brushes.Green : Brushes.Red;
        }
    }
    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        IsOn1 = !IsOn1;
    }
}
}
PaperClip
  • 39
  • 5
  • 3
    if you need persistent state, save (somewhere outside app) current values on exit and read&restore values on next run – ASh Jun 08 '17 at 15:16
  • There is a `ToggleButton` in wpf, use its `IsChecked` and change color [with the trigger](https://stackoverflow.com/a/29989762/1997232). Using binding and serializing/deserializing source somehow (see [this](https://stackoverflow.com/q/3784477/1997232)) is the right way to achieve what you want. – Sinatr Jun 08 '17 at 15:17

1 Answers1

0

First you need to add go to your project settings and add a property: like that You can enter the name of the color or the hex value ("#FF007CE4")

Then you bind the color to the property:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:properties="clr-namespace:WpfApplication1.Properties"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Name="button" Width="100" Height="30" Background="{Binding Source={x:Static properties:Settings.Default}, Path=Color1, Mode=TwoWay}" Click="button_Click" />
    </Grid>
</Window>

To set the color:

private void button_Click(object sender, RoutedEventArgs e)
{
   if(Properties.Settings.Default.Color1 == "White")
   {
      Properties.Settings.Default.Color1 = "Black";
   }
   else
   {
      Properties.Settings.Default.Color1 = "White";
   }
}

Edit:

To save the change of the color you need:

Properties.Settings.Default.Save();
RoJaIt
  • 451
  • 3
  • 10
  • You could also change the property type from `string` to `System.Windows.Media.SolidColorBrush` to continue using `Brushes.Red`. – RoJaIt Jun 08 '17 at 16:39
  • Your answer helped a lot. I was looking for a way to save this settings without actually writing serialization/deserialization. If I want to use this method for (let's say) 10 buttons, do I have to make 10 separate settings? – PaperClip Jun 09 '17 at 10:32
  • According to this [Question](https://social.msdn.microsoft.com/Forums/vstudio/de-DE/c52b4fc6-66be-44ce-8a65-ab548f6f4f04/add-user-properties-settings-at-run-time?forum=csharpgeneral) you can add the Properties programmatically. `foreach( var button in this.Controls.OfType – RoJaIt Jun 09 '17 at 13:07