c# - Saving user color settings of a clicked Button in WPF -
i have little problem saving properties of buttons. buttons small , variety of colors. when press 1 button, specified colors changing... , want save them next start up. textbox values can save them ...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 need last clicked button saved because have allot of colors , maybe .raiseevent can here.
this how looks like:
those 3 little buttons:
- white
- blue
- red
are changing of program. @ every start, default back.
you can store color simple string , typeconverter
automatically converts type brush
. below example.
binding default value 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 code:
private void button_click(object sender, routedeventargs e) { workwithsettings.properties.settings.default.setting = "#ff007ce4"; }
note:
setting - type of string
.
more information can see here:
edit:
below i'll show example, hope you.
so, go settings of project: project -> properties -> parameters
. opens window of approximately:
here have property buttoncolor
, defined in settings. example, took button
, changes background, depending on color of pressed button.
in order property background
synchronize settings 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, set background color @ button, change parameter settings, this:
private void blue_click(object sender, routedeventargs e) { workwithsettings.properties.settings.default.buttoncolor = "blue"; }
to save changes settings, need call method save()
:
private void save_click(object sender, routedeventargs e) { workwithsettings.properties.settings.default.save(); }
now, next time start program, color 1 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
Comments
Post a Comment