1

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.

Thanks.

Leszek Mazur
  • 2,443
  • 1
  • 14
  • 28
  • Have you seen https://stackoverflow.com/questions/3784477/c-sharp-approach-for-saving-user-settings-in-a-wpf-application - your problem is a duplicate of it even though the specifics of your proposed solution may not be – Caius Jard Jun 30 '20 at 06:27
  • fromRGB needs 3 arguments. split text, convert as integer and apply it. – Arphile Jun 30 '20 at 06:30
  • how does the content of `TextEditorBackGround.txt` look like? – Mong Zhu Jun 30 '20 at 06:55
  • The content of TextEditorBackGround.txt would either be a hex code or decimal. – Virtualization Jun 30 '20 at 06:55
  • @Virtualization that doesn't explain what the contents are at all - all it says is that the content is mixed. What is the *actual* content? If you check the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.windows.media.color.fromrgb?view=netcore-3.1) or just hover over `FromRgb` you'll see it expects three numbers, not a string. You have to parse that string and extract the elements – Panagiotis Kanavos Jun 30 '20 at 07:13
  • What are you trying to do though? Both Winforms and WPF allow binding control properties like colours and labels to resources that may come from resource files or even text files. In WPF you can actually use data binding to bind *any* property to any kind of data. Plus, WPF offers theming out-of-the-box [with styles](https://learn.microsoft.com/en-us/dotnet/desktop-wpf/fundamentals/styles-templates-overview). If you want to theme your charts, there may be a built-in solution already – Panagiotis Kanavos Jun 30 '20 at 07:17
  • In fact AvalonEdit already defines several styles you can override to customise it. In your case though, you may only need to add a `Style` tag in your page with rules and values loaded from a file. The easiest would be to [load a XAML resource file](https://learn.microsoft.com/en-us/windows/uwp/design/controls-and-patterns/resourcedictionary-and-xaml-resource-references#merged-resource-dictionaries) - this is explained in [this question](https://stackoverflow.com/questions/10978079/load-control-style-from-separate-file-in-wpf) – Panagiotis Kanavos Jun 30 '20 at 07:25

2 Answers2

1

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));
SandiaDeDia
  • 291
  • 2
  • 9
0

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.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Why not use WPF resources? For starters all this code should be a `Style` tag in your page, with only the value loaded from data (any data) and updated through data binding. WPF already supports loading resources from other XAML files. – Panagiotis Kanavos Jun 30 '20 at 07:25