Is there a way to prevent Selection change in RadListBox?

4 posts, 0 answers
  1. Dmitry
    Dmitry avatar
    20 posts
    Member since:
    Apr 2011

    Posted 05 Jun 2014 Link to this post

    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?
  2. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 06 Jun 2014 Link to this post

    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.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Dmitry
    Dmitry avatar
    20 posts
    Member since:
    Apr 2011

    Posted 06 Jun 2014 in reply to Kalin Link to this post

    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?
  5. Kalin
    Admin
    Kalin avatar
    1207 posts

    Posted 10 Jun 2014 Link to this post

    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.
     
Back to Top