DropDownList item Visibility

6 posts, 2 answers
  1. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 31 Aug 2010 Link to this post

    Hello,

    In the old ListControl i was using the item.Visibility Collapsed to hide some items while some conditions are not met, in the new version with the DropDownList i've noticed that the visibility property no longer applies to the item, it applies to the visual item, and it no longer works as before, and even more than this the IsVisible property of the item is readOnly, can someone please tell me how should i hide an item and not leave a blank space in the list ?

    Thank you very much,

    Best Regards,
    Emanuel Varga
  2. Niklas
    Niklas avatar
    7 posts
    Member since:
    Apr 2009

    Posted 01 Sep 2010 Link to this post

    I got the same problem...

    //Niklas
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Victor
    Admin
    Victor avatar
    1351 posts

    Posted 02 Sep 2010 Link to this post

    Hello Emanuel Varga, Niklas,

    Thank you for writing.

    The approach you were using with the previous combo box and list box do not work with RadListControl and RadDropDownList because of the newly introduced UI virtualization. Now the UI items are transient and you can not rely directly on them to set their state as they are sometimes recreated and/or recycled.
     
    In order to hide items that do not match your criteria you should use the filtering feature. You simply set a Predicate<RadListDataItem> method that you have defined to the Filter property of RadDropDownList. This method will be called for every item in your drop down list and the items for which you return false from the predicate will be hidden from view.

    Write again if you need further assistance.
     
    Kind regards,
    Victor
    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
  5. Jason Parrish
    Jason Parrish avatar
    87 posts
    Member since:
    Sep 2009

    Posted 07 Dec 2010 Link to this post

    Can you provide an example?
  6. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 08 Dec 2010 Link to this post

    Hello Jason,

    Please take a look at the following example:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    public partial class Form1 : Form
    {
        private RadListControl radListControl1;
        private List<string> availableItems = new List<string>
        {
            "a1", "a2", "a3", "b1", "b2", "c1"
        };
     
        private List<string> selectionItems = new List<string>
        {
            "a", "b", "c"
        };
     
        private RadDropDownList radDropDownList;
     
        public Form1()
        {
            InitializeComponent();
     
            this.Controls.Add(radListControl1 = new RadListControl());
            radListControl1.Dock = DockStyle.Fill;
            radListControl1.Filter += OnListFilter;
     
            this.Controls.Add(radDropDownList = new RadDropDownList());
            radDropDownList.Dock = DockStyle.Top;
            radDropDownList.SelectedValueChanged += new EventHandler(radDropDownList_SelectedValueChanged);
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            radDropDownList.DataSource = selectionItems;
            radListControl1.DataSource = availableItems;
        }
     
        private bool OnListFilter(RadListDataItem item)
        {
            return !string.IsNullOrEmpty(radDropDownList.SelectedText) && item.Text.Contains(radDropDownList.SelectedText);
        }
     
        void radDropDownList_SelectedValueChanged(object sender, EventArgs e)
        {
            radListControl1.Rebind();
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
    Telerik WinForms MVP
  7. Answer
    Peter
    Admin
    Peter avatar
    1148 posts

    Posted 11 Dec 2010 Link to this post

    Hi Jason,

    Thank you for writing.

    You can also use the following quick solution to hide a particular item:

    this.radDropDownList1.VisualListItemFormatting += new VisualListItemFormattingEventHandler(radDropDownList1_VisualListItemFormatting);
    this.radDropDownList1.AutoSizeItems = true;
     
    void radDropDownList1_VisualListItemFormatting(object sender, VisualItemFormattingEventArgs args)
    {
         if (args.VisualItem.Data.RowIndex == 2)//collapse the element with index 2
                args.VisualItem.Visibility = ElementVisibility.Collapsed;
    }

    As you can see in the code snippet above, you can handle the VisualListItemFormatting event and set item Item visibility to collapsed depending on a custom condition. In case you decide to follow this approach, you should also set AutoSizeItems to true as well.

    I hope this helps.

     

    Kind regards,
    Peter
    the Telerik team
    Get started with RadControls for WinForms with numerous videos and detailed documentation.
Back to Top
UI for WinForms is Visual Studio 2017 Ready