Find item by text

5 posts, 1 answers
  1. StrandedPirate
    StrandedPirate avatar
    32 posts
    Member since:
    Nov 2007

    Posted 16 Jul 2010 Link to this post

    I'm sure this has been asked before but I couldn't find it; ironic. How do I find the RadMenuItem for a RadContextMenu by the text displayed in it's header?

    My RadContextMenu is not data bound its simply static xaml markup.
  2. Answer
    Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 19 Jul 2010 Link to this post

    Hi Joey,

    If you menu is not bound then all you have to do is to enumerate the Items property, cast the objects to RadMenuItem and then cast the Header property to string.

    Let us know if you need more information.

    Greetings,
    Hristo
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. StrandedPirate
    StrandedPirate avatar
    32 posts
    Member since:
    Nov 2007

    Posted 20 Jul 2010 Link to this post

    I was hoping this API of yours had this basic service baked into a method already. Since it doesn't here's my vote of one for it to be added.
  5. StrandedPirate
    StrandedPirate avatar
    32 posts
    Member since:
    Nov 2007

    Posted 21 Jul 2010 Link to this post

    For anyone who wants to add this functionality to both menu types from Telerik. Note: this only works for non-databound menu's.

        public static class RadMenuExtensions

        {

            /// <summary>

            /// Finds an item by its text.

            /// </summary>

            /// <param name="menu">The menu.</param>

            /// <param name="text">The text.</param>

            /// <returns></returns>

            public static RadMenuItem FindByText(this MenuBase menu, string text)

            {

                RadMenuItem foundItem = null;

                foreach (RadMenuItem item in menu.Items)

                {

                    // watch out for seperators with null headers

                    if (item.Header != null)

                    {

                        if (item.Header.ToString().ToLowerInvariant().Equals(text.ToLowerInvariant()))

                            foundItem = item;

                    }

                }

                return foundItem;

            }

        }

  6. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 21 Jul 2010 Link to this post

    Hello Joey,

    This is the modified code so that it will work with bound menu too:
    public static class RadMenuExtensions
    {
        /// <summary>
        /// Finds an item by its text.
        /// </summary>
        /// <param name="menu">The menu.</param>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public static RadMenuItem FindByText(this MenuBase menu, string text)
        {
            RadMenuItem foundItem = null;
      
            foreach (object item in menu.Items)
            {
                RadMenuItem menuItem = menu.ItemContainerGenerator.ContainerFromItem(item) as RadMenuItem;
                string header = Convert.ToString(menuItem.Header);
      
                if (header.ToLowerInvariant().Equals(text.ToLowerInvariant()))
                {
                    foundItem = menuItem;
                    break;
                }           
            }
      
            return foundItem;
        }
    }

    I hope that this will help you.

    All the best,
    Hristo
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top
DevCraft banner