C1 WPF C1FlexGrid设置样式技巧:单元格前景色和字体设置

<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 == nullreturn;产品

//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 == nullreturn;

//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

PS: 关于ComponentOne,这些产品你能够关注>>
本文转载自葡萄城

有用(0)

相关文章
相关标签/搜索