gpt4 book ai didi

css - Kendo UI Column Center自定义MVC扩展方法

转载 作者:行者123 更新时间:2023-11-28 11:09:29 24 4
gpt4 key购买 nike

我正在寻找一种方法来为 KendoUI 列创建自定义扩展方法,这将允许我使用扩展方法轻松地将 css 类添加到标题或单元格本身。

现在我正在使用以下内容将标题和单元格的内容居中:

columns.Bound(x => x.Value).HeaderHtmlAttributes(new { @class = "cent" }).HtmlAttributes(new { @class = "cent" });

cent css 类在哪里:

.cent { text-align:center; }

在我看来,要为这么简单的事情编写大量代码。

我正在寻找的是一种创建自定义列构建器扩展方法的解决方案,我可以在其中执行如下操作:

columns.Bound(x => x.Value).CenterHeader().CenterCell();

其中 CenterHeader() 和 CenterCell() 将是自定义扩展方法,可将此 css 类分别添加到标题和单元格。如果这是可能的 IMO,代码看起来会更清晰。

有谁知道这样做的方法吗?

最佳答案

为什么不直接创建这些扩展?

using Kendo.Mvc.UI.Fluent;

namespace MyNamespace
{
public static class MyExtensions
{
public static GridTemplateColumnBuilder<T> CenterHeader<T>(this GridTemplateColumnBuilder<T> builder) where T : class
{
return builder.HeaderHtmlAttributes(new { @class = "cent" });
}

.....
}
}

并在 Views/web.config 中添加此命名空间(这样您就不会一直导入它):

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
......
<add namespace="Kendo.Mvc.UI"/>
<add namespace="MyNamespace"/>
......
</namespaces>
</pages>
</system.web.webPages.razor>

关于css - Kendo UI Column Center自定义MVC扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25393692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com