Skip to content

支持复杂类型的单元格格式

对于文本输入框、按钮等普通的单元格类型,您可以直接使用 支持单元格样式 功能实现单元格样式功能。

但是有些复杂类型的单元格,比如表格等同一个单元格类型内,需要对不同的模块分别设置单元格样式,那么就需要使用到本章方式了。

本例以一个表格单元格的Demo代码为例,说明如何在表格上,分别为列头和列以及其他列设置单元格格式。

1725871675834-7242b431-990c-4301-9fa1-38d32719ffca.png

如上图所示,单元格类型可以为多个列以及同一个列的列头和列内容分别设置单元格格式,上述示例的代码如下所示:

csharp
[Icon("pack://application:,,,/Test;component/Resources/Icon.png")]
[Designer("Test.Designer.TestTableCellTypeDesigner, Test")]
public class TestTableCellType : CellType
{
    [DisplayName("列设置")]
    [ObjectListProperty(ItemType = typeof(TestTableColumnConfig), DefaultName = "列")]
    public List<INamedObject> Columns { get; set; } = new List<INamedObject>();
}

public class TestTableColumnConfig : ObjectPropertyBase, INamedObject
{
    public string Name { get; set; }

    [DisplayName("列头样式")]
    [CellFormatSetting]
    public ForguncyStyleInfo HeaderStyleFormat { get; set; }

    [DisplayName("列样式")]
    [CellFormatSetting]
    public ForguncyStyleInfo StyleFormat { get; set; }
}

您可以使用 ObjectListProperty类型的属性作为表格的列定义,然后在属性内,使用 ForguncyStyleInfo类型的子属性定义每一个列的单元格格式信息,从而实现为一个复杂类型的单元格类型中的不同模块分别设置单元格格式功能。

  1. FormatTabType:表示单元格格式设置所支持的Tab栏类型,默认为 PluginFormatDialogTabType.Normal 表示全部开启。您可以如下配置所示隐藏 数字Tab栏:
csharp
[Icon("pack://application:,,,/Test;component/Resources/Icon.png")]
[Designer("Test.Designer.TestTableCellTypeDesigner, Test")]
public class TestTableCellType : CellType
{
    [DisplayName("列设置")]
    [ObjectListProperty(ItemType = typeof(TestTableColumnConfig), DefaultName = "列")]
    public List<INamedObject> Columns { get; set; } = new List<INamedObject>();
}

public class TestTableColumnConfig : ObjectPropertyBase, INamedObject
{
    public string Name { get; set; }

    [DisplayName("列头样式")]
    [CellFormatSetting(
        FormatTabType = PluginFormatDialogTabType.Normal ^ PluginFormatDialogTabType.Number)]
    public ForguncyStyleInfo HeaderStyleFormat { get; set; }
}

1725848538686-969ccaaf-3422-43de-b485-b7a10e97c72e.png

  1. AlignmentFormatOptions:表示对齐设置Tab栏中支持的配置项,默认值为 PluginAlignmentFormatOptions.All 表示全部开启。您可以如下配置所示隐藏 水平对齐设置项:
csharp
[Icon("pack://application:,,,/Test;component/Resources/Icon.png")]
[Designer("Test.Designer.TestTableCellTypeDesigner, Test")]
public class TestTableCellType : CellType
{
    [DisplayName("列设置")]
    [ObjectListProperty(ItemType = typeof(TestTableColumnConfig), DefaultName = "列")]
    public List<INamedObject> Columns { get; set; } = new List<INamedObject>();
}

public class TestTableColumnConfig : ObjectPropertyBase, INamedObject
{
    public string Name { get; set; }

    [DisplayName("列头样式")]
    [CellFormatSetting(
        AlignmentFormatOptions = PluginAlignmentFormatOptions.All ^ PluginAlignmentFormatOptions.HorizontalAlignment)]
    public ForguncyStyleInfo HeaderStyleFormat { get; set; }
}

1725848772248-e4342720-543a-4b5b-b4a5-1b3a6f182269.png

单元格格式的返回值最终会保存在 ForguncyStyleInfo类型的属性中,此类型的各个字段的意思分别是:

属性名说明类型
BackgroundStr背景颜色string 类型
BorderLeft左边框ForguncyBorderLine类型,包含两个属性:
1. ColorStr,字符串类型,表示边框颜色
2. ForguncyBorderLineStyle,枚举类型,表示边框线性
BorderTop上边框
BorderRight右边框
BorderBottom下边框
FontFamily字体string 类型
FontSize字号string 类型
FontStyle字体倾斜string 类型
FontWeight字体加粗string 类型
ForegroundStr字体颜色string 类型
FormatString数字格式字符串string 类型
Strikethrough是否右删除线bool 类型
TextIndent缩进int 类型
Underline是否有下划线bool 类型
WordWrap是否可以自动换行bool 类型
ShrinkToFit是否自动字体缩小填充bool 类型
Ellipsis超出文本是否展示省略号bool 类型
HorizontalAlignment水平对齐方式ForguncyCellHorizontalAlignment枚举类型
VerticalAlignment垂直对齐方式
DisplayTab打开后默认的展示Tab栏PluginFormatDialogTabType枚举类型

通过上述配置,您在运行时就可得到如下所示的配置对象

INFO

{

"backgroundStr": "Accent 6 40",

"fontFamily": "Body",

"fontSize": 18.666666666666664,

"fontWeight": "bold",

"strikethrough": false,

"textIndent": 0,

"underline": false,

"wordWrap": false,

"shrinkToFit": false,

"ellipsis": false,

"horizontalAlignment": 1,

"verticalAlignment": 1

}

在运行时,您需要将上述设置时的参数自行解析为前端的样式,根据前端的框架不同,设置的方式可能各有不同。

需要注意的点:

  1. 格式设置的属性必须为 ForguncyStyleInfo 类型
  2. 运行时的参数是以小驼峰的形式传递的
  3. 此特性为 10.0.100.0 版本新增

更新: 2024-09-09 16:56:06
原文: https://www.yuque.com/robert-bh51n/ea8l6c/zxfaywmlqeod9itg