C1 WPF C1FlexGrid设置样式技巧:单元格前景色和字体设置
来源:互联网 发布:js删除div内容 编辑:程序博客网 时间:2024/06/05 08:17
<ComponentOne Studio for WPF下载>
在之前我们讨论过给单元格设置背景色:通过重写ApplyCellStyles方法,然后设置Border的Background属性实现。本文就在此基础上讨论如何对单元格字体进行设置。
还是通过ApplyCellStyles方法,我们可以拿到Border,然后从Border.Child拿到TextBlock,就可以通过TextBlock的Font相关属性(Foreground,FontWeight, TextDecorations等)设置字体。
前景色
因此设置单元格的前景色和背景色的代码参考如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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;
}
}
代码效果如下:
字体样式设置
再此基础上,我们来讨论字体的设置,只需设置属性即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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相关属性重置。代码参考:
1
2
3
4
5
6
if
(selected)
{
_textblock.FontSize = 12d;
_textblock.FontWeight = FontWeights.Normal;
_textblock.FontStyle = FontStyles.Normal;
}
注意:需要在方法的最后进行Invalidate操作。
代码如下:
1
grid.Invalidate(
new
CellRange(3, 2));
这个时候选择单元格的结果如图:
本文的示例请下载:Wpf_Flex_CellstyleOnCell_update.zip
PS: 关于ComponentOne,这些产品你可以关注>>
本文转载自葡萄城
0 0
- C1 WPF C1FlexGrid设置样式技巧:单元格前景色和字体设置
- C1 WPF C1FlexGrid设置样式技巧:单元格设置背景色
- Extjs grid设置单元格字体颜色,及单元格背景色
- Extjs grid设置单元格字体颜色,及单元格背景色
- Extjs grid设置单元格字体颜色,及单元格背景色
- POI中设置Excel单元格格式样式(居中,字体,边框,背景色、列宽、合并单元格等)
- POI中设置Excel单元格格式样式(居中,字体,边框,背景色、列宽、合并单元格等)
- 设置背景色和前景色
- 文档前景色和背景色设置
- wpf后台设置颜色(背景色,前景色)
- wpf后台设置颜色(背景色,前景色)
- 使用jxl合并单元格,设置字体样式
- 设置单元格背景色
- jxl 设置excel 字体 背景色 合并单元格 列宽
- jxl操作excel(合并单元格,设置背景色,字体颜色)
- jxl 设置excel 字体 背景色 合并单元格 列宽
- jxl 设置excel 字体 背景色 合并单元格 列宽
- DevGridControl单元格背景色和单元格文字颜色设置
- windows下删除超长文件以及文件夹
- 通过xml方式根据word模板导出word
- Linux一键安装web环境全攻略phpstudy版
- Android之通过资源名称获取资源Id
- Java虚拟机运行时数据区
- C1 WPF C1FlexGrid设置样式技巧:单元格前景色和字体设置
- abstract class和interface的区别
- 错误:-bash: phpstudy: command not found(linux phpStudy)
- Hive之——metastore三种配置方式
- AndroidStudio配色方案和Sexy Editor插件
- Java NIO系列教程(四) Scatter/Gather
- java 双层for的break continue代码精简
- 设计模式 —— 代理模式(Proxy Pattern)
- css3多列