Cancel combobox selection in WPF with MVVM

后端 未结 12 1507
你的背包
你的背包 2020-12-08 10:23

I\'ve got a combobox in my WPF application:



        
12条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 10:45

    I did it in a similar way to what splintor has above.

    Your view:

    
    

    Below is the code for the event handler "ComboBox_SelectionChanged" from the code file behind the view. For example, if you view is myview.xaml, the code file name for this event handler should be myview.xaml.cs

    private int previousSelection = 0; //Give it a default selection value
    
    private bool promptUser true; //to be replaced with your own property which will indicates whether you want to show the messagebox or not.
    
    private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                ComboBox comboBox = (ComboBox) sender;
                BindingExpression be = comboBox.GetBindingExpression(ComboBox.SelectedValueProperty);
    
                if (comboBox.SelectedValue != null && comboBox.SelectedIndex != previousSelection)
                {
                    if (promptUser) //if you want to show the messagebox..
                    {
                        string msg = "Click Yes to leave previous selection, click No to stay with your selection.";
                        if (MessageBox.Show(msg, "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) //User want to go with the newest selection
                        {
    
                            be.UpdateSource(); //Update the property,so your ViewModel will continue to do something
                            previousSelection = (int)comboBox.SelectedIndex;  
                        }
                        else //User have clicked No to cancel the selection
                        {
                            comboBox.SelectedIndex = previousSelection; //roll back the combobox's selection to previous one
                        }
                    }
                    else //if don't want to show the messagebox, then you just have to update the property as normal.
                    {
                        be.UpdateSource();
                        previousSelection = (int)comboBox.SelectedIndex;
                    }
                }
            }
    

提交回复
热议问题