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

ListView SelectedItemChanged Question about item selected

1 Answer 572 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
Mark asked on 24 Oct 2016, 05:24 PM

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

1 Answer, 1 is accepted

Sort by
0
Dimitar
Telerik team
answered on 25 Oct 2016, 11:40 AM
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.
Tags
ListView
Asked by
Mark
Top achievements
Rank 2
Bronze
Iron
Veteran
Answers by
Dimitar
Telerik team
Share this question
or