ListView SelectedItemChanged Question about item selected

2 posts, 0 answers
  1. Mark
    Mark avatar
    35 posts
    Member since:
    Oct 2014

    Posted 24 Oct Link to this post

    I have a list view and I am accessing the SelectedItemChanged event.  If there is a specified condition, I don't want the item I selected to be selected and revert back to the item that was selected, if one was selected.  For the most part, I have this working, however, I still get a BOX around the item I don't want selected, even though I clear out the Selected Items object on the ListView.  See the attached GIF, you will notice how the item I selected has a BOX around it. 

     

    These are the 4 lines of code I have tried, even all at once and still, the items has a BOX around it.

    radListViewRoles.SelectedIndex = 0;
    radListViewRoles.SelectedItems.Clear();            
    radListViewRoles.CurrentItem = null;            
    radListViewRoles.SelectedItem = null;

     

    Thanks

  2. Dimitar
    Admin
    Dimitar avatar
    1415 posts

    Posted 25 Oct Link to this post

    Hi Mark,

    Thank you for writing.

    We will introduce a new event in the upcomming service pack (later this week) which will allow you to handle this case. You will be able to cancel the selction like this:
    private void ListViewElement_SelectedItemChanging(object sender, Telerik.WinControls.UI.ListViewItemCancelEventArgs e)
    {
     
        if (radListView1.Items.IndexOf(e.Item) < 10)
        {
            e.Cancel = true;
     
            RadMessageBox.Show("test");
        }
    }

    With the current version you need to override the ProcessSelection method in the SimpleListViewElement class:
    class MyRadListView : RadListView
    {
        protected override RadListViewElement CreateListViewElement()
        {
            return new MyListViewElement();
        }
    }
    class MyListViewElement : RadListViewElement
    {
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(RadListViewElement);
            }
        }
        protected override BaseListViewElement CreateViewElement()
        {
     
            if (this.ViewType == ListViewType.ListView)
            {
                return new MyElement(this);
            }
            return base.CreateViewElement();
        }
    }
    class MyElement : SimpleListViewElement
    {
        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(SimpleListViewElement);
            }
        }
        public MyElement(RadListViewElement owner) : base(owner)
        { }
        protected override void ProcessSelection(ListViewDataItem item, Keys modifierKeys, bool isMouseSelection)
        {
            if (this.Items.IndexOf(item) < 10)
            {
                RadMessageBox.Show("test");
                return;
            }
     
            base.ProcessSelection(item, modifierKeys, isMouseSelection);
        }
    }

    I hope this will be useful.

    Regards,
    Dimitar
    Telerik by Progress
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top