2009-11-03 78 views
1

System.Web.UI.DataVisualization.Charting.Chart控件中,字體可以通過引用Font的姓氏來設置。我如何在代碼中做類似的事情?如何從字符串中獲取字體?

<asp:Chart runat="server"> 
    <legends> 
     <asp:Legend Font="Microsoft Sans Serif, 8.25pt, style=Bold"/> 
    </legends> 
</asp:Chart> 

如何在代碼隱藏中做類似的事情?

chart.Legends[0].Font = Font.???("Microsoft Sans Serif, 8.25pt, style=Bold") 

回答

5

使用one of the constructorsSystem.Drawing.Font類:

chart.Legends[0].Font = new Font("Microsoft Sans Serif", 
           8.25, 
           FontStyle.Bold); 

確保包括System.Drawing獲得輕鬆訪問所有相關項目(FontFamilyFontStyle等)。

+0

+1但我更喜歡: 新字體(新的FontFamily( 「Comic Sans字體MS」),8.25,FontStyle.Bold | FontStyle.BlinkText); – 2009-11-03 20:02:15

+0

@Chris Ballance:-100萬BlinkText。 +1爲漫畫三。 – 2009-11-03 20:03:24

+0

這會起作用,但它並沒有完全回答這個問題,我想也許在框架中會有一些東西會消耗整個字符串並解析它。 :) – Dave 2009-11-03 20:21:45

1

使用System.Drawing.Font構造以下過載:

chart.Legends[0].Font = new Font("Microsoft Sans Serif", 8.25, FontStyle.Bold); 
2

您可能能夠解析它,假設它總是排在這種形式:

string[] fontStrings = "Microsoft Sans Serif, 8.25pt, style=Bold".Split(','); 
fontStrings[1] = fontStrings[1].Replace("pt", ""); 
fontStrings[2] = fontStrings[2].Replace("style=", ""); 
var font = new System.Drawing.Font(
    fontStrings[0], 
    float.Parse(fontStrings[1]), 
    ((FontStyle)Enum.Parse(typeof(FontStyle), fontStrings[2])) 
); 

編輯:啊,我做到了這一點。如果它不是動態的,其他答案明顯比我的字符串更好。 :)

相關問題