2013-07-13 71 views
2

我將System.Media.Brush轉換爲System.Drawing.Brush,但在更改顏色後。它會在轉換器上引發「令牌無效」錯誤。C#WPF令牌在BrushConverter上無效

private Brush DrawingColorToBrush(System.Drawing.Color color) 
    { 
     Brush ret; 

     BrushConverter m; 


     m = new BrushConverter(); 
     ret = (Brush)m.ConvertFromString(color.ToArgb().ToString("X8")); 

     return ret; 
    } 

顏色從System.Windows.Forms.ColorDialog enter image description here

回答

3

,如果你改變你的方法,這是你的代碼將工作...

private Brush DrawingColorToBrush(System.Drawing.Color color) 
    { 
     Brush ret = null; 
     BrushConverter m = new BrushConverter(); 
     string s = "#" + color.ToArgb().ToString("X8"); 
     if (m.CanConvertFrom(typeof (string))) 
     { 
      ret = (Brush) m.ConvertFromString(s); 
     } 
     return ret; 
    } 

的關鍵是未來以「#」字符作爲前綴。

+1

當我已經知道正確的格式時,錯過了這一點是愚蠢的。謝謝! – John