This is a migrated thread and some comments may be shown as answers.

Find item by text

4 Answers 65 Views
Menu
This is a migrated thread and some comments may be shown as answers.
StrandedPirate
Top achievements
Rank 1
StrandedPirate asked on 16 Jul 2010, 02:11 PM
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.

4 Answers, 1 is accepted

Sort by
0
Accepted
Hristo
Telerik team
answered on 19 Jul 2010, 08:04 AM
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
0
StrandedPirate
Top achievements
Rank 1
answered on 21 Jul 2010, 03:37 AM
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.
0
StrandedPirate
Top achievements
Rank 1
answered on 21 Jul 2010, 06:26 AM
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;

        }

    }

0
Hristo
Telerik team
answered on 21 Jul 2010, 11:40 AM
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
Tags
Menu
Asked by
StrandedPirate
Top achievements
Rank 1
Answers by
Hristo
Telerik team
StrandedPirate
Top achievements
Rank 1
Share this question
or