<ComponentOne Studio for WPF下載>ide
在以前咱們討論過給單元格設置背景色:經過重寫ApplyCellStyles方法,而後設置Border的Background屬性實現。本文就在此基礎上討論如何對單元格字體進行設置。post
仍是經過ApplyCellStyles方法,咱們能夠拿到Border,而後從Border.Child拿到TextBlock,就能夠經過TextBlock的Font相關屬性(Foreground,FontWeight, TextDecorations等)設置字體。字體
所以設置單元格的前景色和背景色的代碼參考以下:spa
public
override
void
ApplyCellStyles(C1FlexGrid grid, CellType cellType, CellRange range, Border bdr)
code
{
orm
var columnindex = range.Column;
blog
var rowindex = range.Row;
ip
var _textblock = bdr.Child
as
TextBlock;
get
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));
這個時候選擇單元格的結果如圖:
本文的示例請下載:Wpf_Flex_CellstyleOnCell_update.zip