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

Checkable Items

3 Answers 82 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Holly
Top achievements
Rank 1
Holly asked on 20 Aug 2015, 06:00 PM

How can I do this programatically?

 

http://docs.telerik.com/devtools/wpf/controls/radmenu/features/checkable-items​

3 Answers, 1 is accepted

Sort by
0
Aneliya Petkova
Telerik team
answered on 21 Aug 2015, 12:21 PM
Hi Holly,

I would suggest you to add image with a tick via ImageUrl property on the item to be selected in ItemClick server-side event of the RadMenu. You can also use "Custom attributes" to set State on every clicked item("checked" or "unchecked") and depending on that state to add the ImageUrl.

Please check the following online demo, which demonstrates similar scenario:

Hope this will be helpful.

Regards,
Aneliya Petkova
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Holly
Top achievements
Rank 1
answered on 21 Aug 2015, 02:10 PM

Hi Aneliya,

 My user wants standard checkboxes.  I was able to add a content template programatically, and add a checkbox and bind it to my child menu items.  Now I am unsure how to get the value of the checkboxes, when they are clicked.  The ItemClick event is not firing on the menu when I check an item on the child men.  What I need to be able to do, is allow the user to check items on the menu then click a submit button, and a RadGrid will be filtered based on the selections.  Here is my code if you can help me.

<telerik:RadMenu ID="handsetMenu" runat="server" OnClientItemClicking="DisableRootClick" OnItemClick="handsetMenu_ItemClick">   
                        <Items>   
                                                                                           
                        </Items>                                                     
                    </telerik:RadMenu>

 

protected void createFilter(int categoryid)
    {
        List<int> productIds = new List<int>();
        DataRow[] productRow = CategoriesProductsData.Tables["Products"].Select("Category_ID = " + 573);

        productIds = productRow.Select(p => int.Parse(p["Product_ID"].ToString())).ToList();

        ITCProductService pService = new TCProductServiceClient();
        var productTuples = (pService.GetProductsAttributes(productIds));

        List<Tuple<int, CustomAttribute>> customAttributes = new List<Tuple<int, CustomAttribute>>();
        foreach (var productTuple in productTuples)
        {
            foreach (var attributeTuple in productTuple.m_Item2)
            {
                var customAttribute = new Tuple<int, CustomAttribute>(productTuple.m_Item1, new CustomAttribute(attributeTuple));
                customAttributes.Add(customAttribute);
            }

        }

        List<CustomAttributeCategory> categories = new List<CustomAttributeCategory>();

        var categoryList = customAttributes.Select(a => a.Item2).Select(a => a.Attribute.Category).GroupBy(a => a.AttributeCategoryId);

        var CatProdList = new List<CustomAttributeCategory>();

        foreach (var category in categoryList)
        {
            var CatProd = new CustomAttributeCategory();

            var prodIDList = from product in customAttributes
                             where product.Item2.Attribute.CategoryId == category.Key
                             select Tuple.Create(product.Item1, product.Item2);

            CatProd.Category = customAttributes.Select(a => a.Item2.Attribute.Category).Where(a => a.AttributeCategoryId == category.Key).FirstOrDefault();
            CatProd.ProdAttributesTuple = new List<Tuple<int, CustomAttribute>>();
            CatProd.ProdAttributesTuple = prodIDList.ToList();

            CatProdList.Add(CatProd);
        }

        foreach (var cat in CatProdList)
        {
            var itemCategory = new RadMenuItem(cat.Category.Name);

            handsetMenu.Items.Add(itemCategory);

            var option = cat.ProdAttributesTuple.GroupBy(a => a.Item2.Attribute.Value).ToList();
            foreach (var attr in option)
            {
                //itemCategory.Items.Add(new RadMenuItem(attr.Key));
                var item = new RadMenuItem(attr.Key);

                itemCategory.Items.Add(item);

                CustomContentTemplate template = new CustomContentTemplate();

                item.ContentTemplate = new CustomContentTemplate();
                template.InstantiateIn(item);
                item.DataBind();

                
                
            }

        }

    }

    protected void handsetMenu_ItemDataBound(object sender, RadMenuEventArgs e)
    {
        DataRowView row = (DataRowView)e.Item.DataItem;
    }


    public class CustomAttributeCategory
    {
        public AttributeCategoryModel Category { get; set; }
        public List<Tuple<int, CustomAttribute>> ProdAttributesTuple { get; set; }
    }

    public class CustomAttribute
    {
        public AttributeModel Attribute { get; set; }

        public List<int> ProductIds { get; set; }

        public CustomAttribute(AttributeModel attribute)
        {
            Attribute = attribute;
            ProductIds = new List<int>();
        }
    }

    protected void rcbMenu_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
    {
        DataRowView row = (DataRowView)e.Item.DataItem;
    }

    class CustomContentTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            CheckBox cb = new CheckBox();
            cb.DataBinding += new EventHandler(cb_DataBinding);
            container.Controls.Add(cb);            
        }

        private void cb_DataBinding(object sender, EventArgs e)
        {
            CheckBox target = (CheckBox)sender;
            RadMenuItem item = (RadMenuItem)target.BindingContainer;
            target.Text = item.Text;
        }
    }

    protected void handsetMenu_ItemClick(object sender, RadMenuEventArgs e)
    {

    }

0
Aneliya Petkova
Telerik team
answered on 24 Aug 2015, 11:01 AM
Hi Holly,

You can get every CheckBox inside RadMenuItem using FindControl method. For example in the onClick event handler of the Button, you can iterate through all RadMenuItems and get the Checked state of the CheckBox inside the item using the following code:
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (RadMenuItem item in RadMenu1.GetAllItems())
    {
        bool isChecked = ((CheckBox)item.FindControl("CheckBox1")).Checked;
    }
}

Hope this will be helpful.

Regards,
Aneliya Petkova
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Menu
Asked by
Holly
Top achievements
Rank 1
Answers by
Aneliya Petkova
Telerik team
Holly
Top achievements
Rank 1
Share this question
or