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
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);
© 2024 OneMinuteCode. All rights reserved.