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

Is there a way to prevent Selection change in RadListBox?

3 Answers 628 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Dmitry
Top achievements
Rank 1
Dmitry asked on 05 Jun 2014, 02:40 PM
Hello!

I am trying to use RadListBox and I have a scenario when I need to prevent selection change in list box (for example user wants to save change he/she made before navigating away from currently selected item). How can I accomplish this?

3 Answers, 1 is accepted

Sort by
0
Kalin
Telerik team
answered on 06 Jun 2014, 11:06 AM
Hello Dmitry,

If I understand you correctly you need to prevent the selection while the saving process is running. What I can suggest you would be to simply set the IsEnabled property of the ListBox to false while saving and set it true again when saved.

Hope this helps. However if this is not the exact case I'll ask you to share more detailed information about the exact scenario.

Regards,
Kalin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Dmitry
Top achievements
Rank 1
answered on 06 Jun 2014, 11:14 AM
No, this is not the exact scenario. It looks like this: ListBox is used to show collection of items, while user selects any of the item he/she can get detailed view of the item in other part of the user control, and user can edit item properties. But if user edited item and did not save changes, and tries to select another item I want to show a confirmation window "Save changes?" with buttons Yes/No/Cancel.
In case of yes/no, I just save or dismiss user's changes and Selection in list box can be changed, but if User clicks Cancel button - I need to prevent selection change in ListBox.
Is there a way to do this using RadListBox, or I should use some other control for such a scenario?
0
Kalin
Telerik team
answered on 10 Jun 2014, 12:38 PM
Hi Dmitry,

Thanks for the further details. What I can suggest you in order to achieve the desired scenario would be to attach MouseButtonDown class handler to all of the ListBoxItems. This way you will be able to "preview" the SelectionChanged, display the confirmation dialog and if you doesn't want to change the SelectedItem you can simply handle the MouseButtonDown event and this will prevent the selection. Please check the following code snippet:

public MainWindow()
{
    InitializeComponent();
    EventManager.RegisterClassHandler(typeof(RadListBoxItem), Mouse.PreviewMouseDownEvent, new MouseButtonEventHandler(OnMouseDownHandler), true);
}
 
private void OnMouseDownHandler(object sender, MouseButtonEventArgs e)
{
    var listBoxItem = sender as RadListBoxItem;
    if (!listBoxItem.IsSelected)
    {
        // open the dialog here and cancel the selection if needed
        e.Handled = true;
    }
}

Hope this will help you achieve the desired.

Regards,
Kalin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
ListBox
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Kalin
Telerik team
Dmitry
Top achievements
Rank 1
Share this question
or