【问题标题】:Saving user color settings of a clicked Button in WPF在 WPF 中保存单击按钮的用户颜色设置
【发布时间】:2013-08-14 03:09:24
【问题描述】:

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

代码:

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 可以在这里提供帮助。

看起来是这样的:

这三个小按钮:

  • 白色
  • 蓝色
  • 红色

用于改变程序的外观。每次启动时,默认值都会返回。

【问题讨论】:

标签: c# wpf button settings


【解决方案1】:

您可能需要在项目的Settings 选项卡中创建存储有关颜色信息的项目。我建议存储十六进制字符串。然后,在MainForm_Load 上检索这些值。

确保也将设置放在User 范围内,否则每次关闭应用程序时它们都会重置。

【讨论】:

    【解决方案2】:

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

    从 XAML 绑定默认值:

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

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

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

    TypeConverters and XAML

    Edit:

    下面我将向您展示一个示例,希望对您有所帮助。

    所以,进入项目的设置:Project -&gt; Properties -&gt; Parameters。这会打开一个大约如下的窗口:

    这里我们有一个属性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();
    }
    

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

    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

    【讨论】:

    • 我不需要存储颜色...颜色太多了,。最后一次单击的按钮如何保存并在下次运行时加载?
    • 好吧,当我按下一个按钮时,该方法内部的所有颜色都会被应用。我想保存它。类似“blueColor_Click(被按下...然后保存并在下次启动时运行)”之类的东西。
    • @user2558921:好的,据我了解:您希望保留控件的颜色,并且下次加载它们。您必须在字符串类型的设置参数下创建每种颜色(参见我的示例)。正如我在您的示例中看到的,您有两种颜色。在设置中创建两个具有两种颜色的参数。您更改值并调用 Save(),然后更改将被保存。
    • 它不起作用,保存代码颜色不会使其在下次启动时处于活动状态。
    • 是的,您的应用程序运行良好,谢谢。我现在需要用您的示例修复我的应用程序 :)
    猜你喜欢
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2019-01-03
    • 2016-07-15
    • 1970-01-01
    相关资源
    最近更新 更多