原創(chuàng)|使用教程|編輯:龔雪|2016-05-30 09:27:17.000|閱讀 747 次
概述:在之前我們討論過(guò)給單元格設(shè)置背景色:通過(guò)重寫(xiě)ApplyCellStyles方法,然后設(shè)置Border的Background屬性實(shí)現(xiàn)。本文就在此基礎(chǔ)上討論如何對(duì)單元格字體進(jìn)行設(shè)置。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
<ComponentOne Studio for WPF下載>
在之前我們討論過(guò)給單元格設(shè)置背景色:通過(guò)重寫(xiě)ApplyCellStyles方法,然后設(shè)置Border的Background屬性實(shí)現(xiàn)。本文就在此基礎(chǔ)上討論如何對(duì)單元格字體進(jìn)行設(shè)置。
還是通過(guò)ApplyCellStyles方法,我們可以拿到Border,然后從Border.Child拿到TextBlock,就可以通過(guò)TextBlock的Font相關(guān)屬性(Foreground,FontWeight, TextDecorations等)設(shè)置字體。
因此設(shè)置單元格的前景色和背景色的代碼參考如下:
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; } }
代碼效果如下:
再此基礎(chǔ)上,我們來(lái)討論字體的設(shè)置,只需設(shè)置屬性即可。
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; } }
這個(gè)時(shí)候,該單元格的背景色,前景色和字體樣式都發(fā)生了改變。非選擇的時(shí)候:
選擇的時(shí)候:
如果這個(gè)時(shí)候希望這個(gè)特定的單元格,在選擇的時(shí)候字體樣式發(fā)生改變(恢復(fù)成未設(shè)置的狀態(tài)),這個(gè)時(shí)候我們就需要添加代碼,在選擇的時(shí)候設(shè)置_textblock的Font相關(guān)屬性重置。代碼參考:
if (selected) { _textblock.FontSize = 12d; _textblock.FontWeight = FontWeights.Normal; _textblock.FontStyle = FontStyles.Normal; }
注意:需要在方法的最后進(jìn)行Invalidate操作。
代碼如下:
grid.Invalidate(new CellRange(3, 2));
這個(gè)時(shí)候選擇單元格的結(jié)果如圖:
本文的示例請(qǐng)下載:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)