The World’s Largest Online Community for Developers

'; wpf - I need help changing a UI element's color with a text file in C# - LavOzs.Com

So, I have made a WPF application, where I want to let the user customize certain objects of the User Interface.

I tried to do this with text files as colour properties, but I couldn't find a way of doing so.

I found that Hexadecimal was easier as an input.

I was thinking of doing it in a method similar to this:

string AvalonBackground = File.ReadAllText("./bin/Theme/TextEditorBackGround.txt");
this.TextEditorAvalon.Background = AvalonBackground; //we cannot implicitly convert a string into a solidcolorbrush, there are better methods of doing so. 

Leave a comment or answer if you have the solution.


Color.FromRgb does not accept a string. Maybe you could do something like this

string AvalonBackground = File.ReadAllText("./bin/Theme/TextEditorBackGround.txt");
string[] words = AvalonBackground.Split(',');
byte r = Convert.ToByte(words[0]);
byte g = Convert.ToByte(words[1]);
byte b = Convert.ToByte(words[2]);
var color = SolidColorBrush(Color.FromRgb(r,g,b));

So, after me not being stupid, I found a solution to my own problem. Here is the rough code for those of you who want it.

string AvalonBackground = File.ReadAllText("./bin/Theme/TextEditorBackGround.txt");
Color color = (Color)ColorConverter.ConvertFromString(AvalonBackground);
SolidColorBrush brush = new SolidColorBrush(color);
this.TextEditorAvalon.Background = brush; 

I feel really stupid since it took me an hour to find a solution to this trash.

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?
How do I save a stream to a file in C#?
Set background color of WPF Textbox in C# code
Why do we need boxing and unboxing in C#?
Rename a file in C#
Hiding and Showing menu (tool) bar based on user access level
How to use char.TryParse with a string
C# WPF: Changing color of Text upon Character Detection
How can I animate Label.Content with a DoubleAnimation?
how to write a custom setter for a dependency object in wpf using mvvm