I want the color I wrote in XML to be the background color of the dynamically generated button.

Asked 1 years ago, Updated 1 years ago, 375 views

We are working on generating buttons dynamically.
I would like to set the background color of the button to the color written in XML (such as "Red"). I'm having a hard time because I don't know how to set the color written in XML to the background color.
If you know, please give me some advice.


You can get the color (string) that you wrote in XML.
You can use the color you decided on in the code behind as the background color, but the color (string) you got from XML is
The background color cannot be set.

 ·
   ・
   ・
    System.Windows.Controls.Buttonbut=newSystem.Windows.Controls.Button();
    but.Background=Brushes.Yellow;// I don't know how to enter the color (string) obtained from XML, so I temporarily set a fixed color.
    This.Buttons.Children.Add(but);                                                             
   ・
   ・
   ・

If you do not have enough information to answer, please let me know.

wpf

2023-01-11 14:00

1 Answers

The following article introduced in the comment will be applicable.
Convert string to Brushes/Brush color name in C#

string BtnBGColor="Yellow"; // For example, if a color string retrieved from XML is included in this variable,

System.Windows.Controls.Buttonbut=newSystem.Windows.Controls.Button();
but.Background=(SolidColorBrush) new BrushConverter().ConvertFromString(BtnBGColor);
This.Buttons.Children.Add(but);


2023-01-11 23:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.