c# - 在 WPF 中保存单击按钮的用户颜色设置

标签 c# wpf button settings

我在保存按钮的某些属性时遇到了一些问题。按钮很小并且有多种颜色。当我按下一个按钮时,一些指定的颜色正在改变......我想保存它们以供下次启动。我可以保存文本框值,但这......我不能。

代码:

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

我想我需要保存最后单击的按钮,因为我已经分配了颜色,也许 .RaiseEvent 可以在这里提供帮助。

它是这样的:

enter image description here

那 3 个小按钮:

  • 白色
  • 蓝色
  • 红色

用于更改程序的外观。每次启动时,都会恢复默认设置。

最佳答案

您可以将颜色存储为简单的字符串,TypeConverter 会自动将其转换为 Brush 类型。下面是一个例子。

从 XAML 绑定(bind)默认值:

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

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

从代码中设置值:

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

注意:设置 - 这只是String的类型。

您可以在此处查看更多信息:

TypeConverters and XAML

编辑:

下面我给大家举个例子,希望对大家有所帮助。

因此,进入项目的设置:项目 -> 属性 -> 参数。这将打开一个大约的窗口:

enter image description here

这里我们有一个在设置中定义的属性ButtonColor。例如,我使用了 Button,它根据按下的按钮的颜色更改背景。

为了使属性Background与设置同步,所以:

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

默认背景颜色为白色。现在,要设置按钮的背景颜色,我们更改参数设置,如下所示:

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

要保存对设置的更改,您需要调用方法Save():

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

现在,下次启动程序时,颜色将是上次设置的颜色。

完整示例

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>

隐藏代码

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

输出

enter image description here

关于c# - 在 WPF 中保存单击按钮的用户颜色设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18193207/

相关文章:

c# - MongoDB 中的正则表达式 + 选项(C# 驱动程序)

c# - 如何让SQL代码生成和返回代码

wpf - 设备无关像素的优势是什么?

android - 动态对齐按钮 - Android

Swift:使用工具栏按钮删除 TableView 中的多个单元格

c# - 如何将整个文件夹添加到 zip 存档

c# - "&#64;"这样的字符是什么类型,如何转换成ASCII

c# - 如何设置多个viewbox最大的尺寸?

wpf - 如何在XAML中按颜色组件定义Silverlight颜色?

JavaFX 按钮递增和递减事件无法正常工作