Itemplate 属性不可用作属性

发布于 2024-09-18 19:50:59 字数 976 浏览 16 评论 0原文

我已向 Telerik 的 RadGrid 控件添加了一个名为 SearchMenuTemplate 的 ITemplate:

    public class AbsRadGrid : RadGrid
{

    private ITemplate _ItemTemplate;
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [TemplateContainer(typeof(searchBar))]
    public ITemplate SearchMenuTemplate
    {
        get { return _ItemTemplate; }// get
        set { _ItemTemplate = value; }// set
    }
}

并且 Template 类看起来类似于(为简洁起见,已省略像 createchildcontrol 这样的强制重写方法):

[ParseChildren(true)]
class searchBar : CompositeControl, INamingContainer
{
    public string rbStartsWithText { get; set; }
}

现在,在源代码管理窗口中,RadGrid 控件可以看到模板。但 rbStartsWithText 不是节点上的属性。

我想看到这样的东西(注意:abs 前缀在标记中注册):

    <abs:AbsRadGrid ID="rg" runat="server">
    <SearchMenuTemplate rbStartsWithText="Starts With" />
</abs:AbsRadGrid>

相反,rbStartsWithText 抛出一个绿色的波浪线并告诉我它不是 SearchMenuTemplate 的有效属性。

I've added an ITemplate to Telerik's RadGrid control called SearchMenuTemplate ala:

    public class AbsRadGrid : RadGrid
{

    private ITemplate _ItemTemplate;
    [PersistenceMode(PersistenceMode.InnerProperty)]
    [TemplateContainer(typeof(searchBar))]
    public ITemplate SearchMenuTemplate
    {
        get { return _ItemTemplate; }// get
        set { _ItemTemplate = value; }// set
    }
}

And the Template class looks something like (mandatory override methods like createchildcontrol have been omitted for brevity):

[ParseChildren(true)]
class searchBar : CompositeControl, INamingContainer
{
    public string rbStartsWithText { get; set; }
}

Now, in the source control window the RadGrid control sees the Template. But rbStartsWithText isn't an attribute on the node.

I want to see something like this (note: abs prefix is registered in the markup):

    <abs:AbsRadGrid ID="rg" runat="server">
    <SearchMenuTemplate rbStartsWithText="Starts With" />
</abs:AbsRadGrid>

Instead rbStartsWithText is throwing a green squiggly and telling me it's not a valid attribute of SearchMenuTemplate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光的暖冬 2024-09-25 19:50:59

您的 SearchMenuTemplate 属性属于 ITemplate 类型,没有公共属性,因此 IntelliSense 无法为您的 标记提供任何属性。

为了能够添加自定义属性,您应该实现 ITemplate 接口(InstantiateIn 方法)并在其中添加所需的属性:

public class YourCustomTemplate : ITemplate
{
    public string rbStartsWithText { get; set; }

    public void InstantiateIn(Control container)
    {
        HtmlGenericControl div = new HtmlGenericControl("div");
        div.InnerText = rbStartsWithText;
        container.Controls.Add(div);
    }
}

然后您可以在自定义网格中使用它:

public class AbsRadGrid : RadGrid
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public YourCustomTemplate  SearchMenuTemplate { get; set; }
}

最后:

<abs:AbsRadGrid ID="rg" runat="server">
    <SearchMenuTemplate rbStartsWithText="Starts With" />
</abs:AbsRadGrid>

Your SearchMenuTemplate property is of a type ITemplate which hasn't public properties, so IntelliSense just cannot offer any attribute for your <SearchMenuTemplate> tag.

To be able to add a custom property you should implement ITemplate interface (InstantiateIn method) and add desired property there:

public class YourCustomTemplate : ITemplate
{
    public string rbStartsWithText { get; set; }

    public void InstantiateIn(Control container)
    {
        HtmlGenericControl div = new HtmlGenericControl("div");
        div.InnerText = rbStartsWithText;
        container.Controls.Add(div);
    }
}

then you could use it your custom grid:

public class AbsRadGrid : RadGrid
{
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public YourCustomTemplate  SearchMenuTemplate { get; set; }
}

and finally:

<abs:AbsRadGrid ID="rg" runat="server">
    <SearchMenuTemplate rbStartsWithText="Starts With" />
</abs:AbsRadGrid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文