Search code examples
c#wpfbuttonbooleanpersistent-data

Persistent button color changes in C# WPF


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

Solution

  • 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();