保存在一个WPF点击的按钮用户颜色设置 [英] Saving user color settings of a clicked Button in WPF
问题描述
我有救了我的按钮的一些性质有点问题。的按钮是小,并用各种颜色。当我preSS一个按钮,一些指定的颜色正在发生变化......,我想救他们下一次启动时。文本框的值,我可以救他们,但这个...我不能。
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");
}
我想我需要在最后单击按钮进行保存,因为我已经配发的颜色,也许是.RaiseEvent可以帮助在这里。
I think I need the last clicked Button to be saved because I have allot of colors and maybe the .RaiseEvent can help here.
这是它的样子:
3的那些小按钮:
- 白色
- 蓝色
- 红色
是用于改变节目的外观。在每次启动时,默认是回来了。
are for changing the look of the program. At every start, the default is back.
推荐答案
您可以将颜色存储为一个简单的字符串和的TypeConverter
自动将其转换为键入刷
。下面是一个例子。
You can store the color as a simple string and TypeConverter
automatically converts it to type Brush
. Below is an example.
这是XAML绑定默认值:
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}" />
从code设置值:
Set value from code:
private void Button_Click(object sender, RoutedEventArgs e)
{
WorkWithSettings.Properties.Settings.Default.Setting = "#FF007CE4";
}
注:
设置 - 这是字符串
的更多信息,你可以在这里看到:
More information you can see here:
编辑:
下面我会告诉你一个例子,我希望能帮助你。
Below I'll show you an example, that I hope will help you.
所以,进入该项目的设置:项目 - &GT;属性 - &GT;参数
。这将打开约一个窗口:
So, go into the settings of the project: Project -> Properties -> Parameters
. This opens a window of approximately:
在这里,我们有一个属性 ButtonColor
,在设置中定义。比如,我把按钮
,它改变的背景下,依赖于pressed按钮的颜色。
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后面
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();
}
}
}
输出
这篇关于保存在一个WPF点击的按钮用户颜色设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!