简体   繁体   English

自定义ASP.NET菜单控件

[英]Custom ASP.NET menu control

I am working on a custom menu control, partially as a learning exercise, and I am having trouble with Visual Studio's IntelliSense support for it in markup view. 我正在做一个自定义菜单控件,部分是作为学习练习,但是在标记视图中使用Visual Studio的IntelliSense支持时遇到了麻烦。

The conventional ASP.NET menu allows you to place an arbitrary depth of <asp:MenuItem/> elements under the <Items>...</Items> element. 常规的ASP.NET菜单允许您在<Items>...</Items>元素下放置任意深度的<asp:MenuItem/>元素。 I'm after the same behaviour for my menu. 我的菜单也有同样的行为。

Mine unfortunately does not. 不幸的是,我没有。 VS insists on an empty tag: VS坚持使用一个空标签:

<hn:AwesomeMenu runat="server" ID="menu">
    <Items />
</hn:AwesomeMenu>

I have used Reflector to dig into the ASP.NET Menu control and its related classes (MenuItem, MenuItemCollection and soforth) and have ensured that my classes have the same interfaces and attributes, so I'm slightly stumped as to where I'm going wrong. 我已经使用Reflector来深入研究ASP.NET Menu控件及其相关类(MenuItem,MenuItemCollection和soforth),并确保我的类具有相同的接口和属性,因此对于要去的地方我有些困惑错误。 Stubs of my classes so far are as follows: 到目前为止,我班的存根如下:

AwesomeMenu.cs AwesomeMenu.cs

public class AwesomeMenu
    : HierarchicalDataBoundControl,
      IPostBackEventHandler,
      INamingContainer
{
    [PersistenceMode(PersistenceMode.InnerProperty),
     MergableProperty(false),
     DefaultValue(default(string)),
     Browsable(false)]
    public AwesomeCollection Items
    {
        get { ... }
    }
}

AwesomeCollection.cs AwesomeCollection.cs

public class AwesomeCollection
    : ICollection,
      IEnumerable,
      IStateManager
{ ... }

AwesomeItem.cs AwesomeItem.cs

[ParseChildren(true, "Children")]
public class AwesomeItem
    : IStateManager,
      ICloneable
{
    [PersistenceMode(PersistenceMode.InnerDefaultProperty),
     MergableProperty(false)]
    public AwesomeCollection Children
    {
        get { ... }
    }

    public AwesomeItem Parent
    {
        get { ... }
    }
}

Interface implementations are omitted for brevity. 为了简洁起见,省略了接口实现。 Any help would be appreciated. 任何帮助,将不胜感激。

Thanks largely to a blog post I belatedly discovered ( The ParseChildren PersistChildren and PersistenceMode.InnerProperty ), I was able to wrap my head around the appropriate usage of the aforementioned attributes. 很大程度上要归功于我后来才发现的一篇博客文章( ParseChildren PersistChildren和PersistenceMode.InnerProperty ),我得以将脑袋围绕上述属性的适当用法。

Basically I only required a [ParseChildren(...)] attribute on the menu class itself. 基本上,我只需要菜单类本身具有[ParseChildren(...)]属性。 A minimal successful implementation of the desired behaviour is as follows: 所需行为的最小成功实现如下:

Menu.cs Menu.cs

[ParseChildren(ChildrenAsProperties = true)]
public class Menu
    : Control
{
    [PersistenceMode(PersistenceMode.InnerProperty),
     Browsable(false)]
    public List<Item> Items { get; set; }
}

Item.cs Item.cs

[ParseChildren(
    typeof(Item),
    DefaultProperty = "Items",
    ChildrenAsProperties = true)]
public class Item
{
    public string Text { get; set; }

    [Browsable(false)]
    public List<Item> Items { get; set; }
}

My only question is how the ASP.NET menu achieves the same behaviour without the use of this attribute. 我唯一的问题是ASP.NET菜单如何在不使用此属性的情况下实现相同的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM