C# Brush to string

前端 未结 3 1937
礼貌的吻别
礼貌的吻别 2020-12-07 04:28

I search a way to save the color of a brush as a string. For example I have a Brush which has the color red. Now I want to write \"red\" in a textbox.

Thanks for any

3条回答
  •  清歌不尽
    2020-12-07 04:54

    Basically I'll post what was already answered.

    string color = textBox1.Text;
    
    // best, using Color's static method
    Color red1 = Color.FromName(color);
    
    // using a ColorConverter
    TypeConverter tc1 = TypeDescriptor.GetConverter(typeof(Color)); // ..or..
    TypeConverter tc2 = new ColorConverter();
    Color red2 = (Color)tc.ConvertFromString(color);
    
    // using Reflection on Color or Brush
    Color red3 = (Color)typeof(Color).GetProperty(color).GetValue(null, null);
    
    // in WPF you can use a BrushConverter
    SolidColorBrush redBrush = (SolidColorBrush)new BrushConverter().ConvertFromString(color);
    

    Original answer: Convert string to Brushes/Brush color name in C#

提交回复
热议问题