Saving user color settings of a clicked Button in WPF

二次信任 提交于 2019-12-19 10:27:52

问题


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.


回答1:


You can store the color as a simple string and TypeConverter automatically converts it to type Brush. Below is an example.

Binding default value from XAML:

xmlns:properties="clr-namespace:WorkWithSettings.Properties"

<Button Width="100" Height="30"
        Background="{Binding Source={x:Static properties:Settings.Default}, Path=Setting, Mode=TwoWay}" />

Set value from code:

private void Button_Click(object sender, RoutedEventArgs e)
{
    WorkWithSettings.Properties.Settings.Default.Setting = "#FF007CE4";
}

Note: Setting - this is just the type of String.

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 the Button, which changes the background, depending on the color of the pressed button.

In order to property Background the synchronize with settings to do, so:

<Button Width="100" Height="30" 
        Content="TestButton" 
        Background="{Binding Source={x:Static properties:Settings.Default}, Path=ButtonColor, Mode=TwoWay}" />

The default background color of white. Now, to set the background color at the button, we change the parameter settings, like this:

private void Blue_Click(object sender, RoutedEventArgs e)
{
    WorkWithSettings.Properties.Settings.Default.ButtonColor = "Blue";
}

To save changes to the settings, you need to call a method Save():

private void Save_Click(object sender, RoutedEventArgs e)
{
    WorkWithSettings.Properties.Settings.Default.Save();
}

Now, the next time you start the program, the color will be the one that was set last.

Full example

XAML

<Window x:Class="WorkWithSettings.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:properties="clr-namespace:WorkWithSettings.Properties"
    WindowStartupLocation="CenterScreen"
    Title="MainWindow" Height="350" Width="525">

    <Grid>
        <TextBlock Width="100" Height="30" Text="{Binding Source={x:Static properties:Settings.Default}, Path=ButtonColor, Mode=TwoWay}" Margin="0,60,0,0" />
        <Button Width="100" Height="30" Content="TestButton" Background="{Binding Source={x:Static properties:Settings.Default}, Path=ButtonColor, Mode=TwoWay}" />

        <WrapPanel>           
            <Button Name="Blue" Width="100" Height="30" Content="BlueColor" VerticalAlignment="Top" Click="Blue_Click" />
            <Button Name="Red" Width="100" Height="30" Content="RedColor" VerticalAlignment="Top" Click="Red_Click" />
            <Button Name="White" Width="100" Height="30" Content="WhiteColor" VerticalAlignment="Top" Click="White_Click" />
        </WrapPanel>

        <Button Name="Save" Width="60" Height="30" Content="Save" VerticalAlignment="Top" HorizontalAlignment="Right" Click="Save_Click" />
    </Grid>
</Window>

Code behind

namespace WorkWithSettings
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void White_Click(object sender, RoutedEventArgs e)
        {
            WorkWithSettings.Properties.Settings.Default.ButtonColor = "White";
        }

        private void Blue_Click(object sender, RoutedEventArgs e)
        {
            WorkWithSettings.Properties.Settings.Default.ButtonColor = "Blue";
        }

        private void Red_Click(object sender, RoutedEventArgs e)
        {
            WorkWithSettings.Properties.Settings.Default.ButtonColor = "Red";
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            WorkWithSettings.Properties.Settings.Default.Save();
        }
    }
}

Output




回答2:


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, on MainForm_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.



来源:https://stackoverflow.com/questions/18193207/saving-user-color-settings-of-a-clicked-button-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!