原創|使用教程|編輯:龔雪|2016-05-30 09:27:17.000|閱讀 747 次
概述:在之前我們討論過給單元格設置背景色:通過重寫ApplyCellStyles方法,然后設置Border的Background屬性實現。本文就在此基礎上討論如何對單元格字體進行設置。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
<ComponentOne Studio for WPF下載>
在之前我們討論過給單元格設置背景色:通過重寫ApplyCellStyles方法,然后設置Border的Background屬性實現。本文就在此基礎上討論如何對單元格字體進行設置。
還是通過ApplyCellStyles方法,我們可以拿到Border,然后從Border.Child拿到TextBlock,就可以通過TextBlock的Font相關屬性(Foreground,FontWeight, TextDecorations等)設置字體。
因此設置單元格的前景色和背景色的代碼參考如下:
public override void ApplyCellStyles(C1FlexGrid grid, CellType cellType, CellRange range, Border bdr) { var columnindex = range.Column; var rowindex = range.Row; var _textblock = bdr.Child as TextBlock; if (_textblock == null) return; //check if the cell is selected or not bool selected=(columnindex == grid.Selection.Column && rowindex == grid.Selection.Row); if ((columnindex == 2) && (rowindex == 3)&&!selected) { //set the customizations on the cell when it is not selected bdr.Background = new SolidColorBrush(Colors.Red); _textblock.Foreground= Brushes.Yellow; } }
代碼效果如下:
再此基礎上,我們來討論字體的設置,只需設置屬性即可。
public override void ApplyCellStyles(C1FlexGrid grid, CellType cellType, CellRange range, Border bdr) { var columnindex = range.Column; var rowindex = range.Row; var _textblock = bdr.Child as TextBlock; if (_textblock == null) return; //check if the cell is selected or not bool selected=(columnindex == grid.Selection.Column && rowindex == grid.Selection.Row); if ((columnindex == 2) && (rowindex == 3)&&!selected) { //set the customizations on the cell when it is not selected bdr.Background = new SolidColorBrush(Colors.Red); _textblock.Foreground= Brushes.Yellow; _textblock.FontSize = 14d; _textblock.FontWeight = FontWeights.Bold; _textblock.FontStyle = FontStyles.Italic; } }
這個時候,該單元格的背景色,前景色和字體樣式都發生了改變。非選擇的時候:
選擇的時候:
如果這個時候希望這個特定的單元格,在選擇的時候字體樣式發生改變(恢復成未設置的狀態),這個時候我們就需要添加代碼,在選擇的時候設置_textblock的Font相關屬性重置。代碼參考:
if (selected) { _textblock.FontSize = 12d; _textblock.FontWeight = FontWeights.Normal; _textblock.FontStyle = FontStyles.Normal; }
注意:需要在方法的最后進行Invalidate操作。
代碼如下:
grid.Invalidate(new CellRange(3, 2));
這個時候選擇單元格的結果如圖:
本文的示例請下載:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網