S52
S52

Reputation: 419

WPF : Close combobox on item selection and display Message box

Goal I have Combobox in WPF and whenever a user selects the item from Combobox I want to close the Combobox (displaying old value as selected value) and display MessageBox with Ok/Cancel button. If a user clicks Ok, the new selected value should be set else it should return.

Problem When I'm selecting item I'm able to display the MessageBox along with Combobox open, which I dont want. As soon as user selects something I want to close the Combobox and display Messagebox.

How I can do it?

XMAL code

 <ComboBox Name="Currency" Grid.Row="1" Grid.Column="5" ItemsSource="{Binding comboboxSource}"
                        SelectedValuePath="Value.bank_currency" IsReadOnly="False" IsTextSearchEnabled="True" TextSearch.TextPath="Value.bank_currency" 
                        SelectedItem="{Binding SelectedBankCurrency, UpdateSourceTrigger=LostFocus,Mode=Twoway}">

C# Code

public KeyValuePair<string, bankCurrencyObject>? SelectedBankCurrency
    {
        get { return _selectedCurrency; }
        set
        {
                MessageBoxResult result = MessageBox.Show("Are you sure you want to change the currency?",
                       "Warning",
                       MessageBoxButton.OKCancel,
                       MessageBoxImage.Question);

                if (result == MessageBoxResult.Cancel)
                {
                    return;
                }
                else
                {
                    //set values
                }
   }
   }

Attempt using selectionChanged event but this did not work,

  private void Combobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (DataContext == null)
            return;
        var combo = (sender as ComboBox);

        if (combo != null && combo.IsDropDownOpen)
        {
            combo.IsDropDownOpen = false;
            var binding = combo.GetBindingExpression(ComboBox.SelectedItemProperty);

            binding.UpdateSource();
            binding.UpdateTarget();
        }
    }`

Upvotes: 2

Views: 3985

Answers (2)

Vyrira
Vyrira

Reputation: 174

You can do this on the SelectionChanged event.

The selectedItem field keeps track of the previously selected item so that it will not show a MessageBox when there is no change of currency. If the selected item has been changed it hides the DropDown menu before showing the MessageBox. Then if the user clicked Cancel it reverts the change otherwise it stores the current selection in selectedItem to compare in the future.

private object selectedItem = null;

private void Currency_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (Currency.SelectedItem == selectedItem)
        return;

    Currency.IsDropDownOpen = false;

    MessageBoxResult result = MessageBox.Show("Are you sure you want to change the currency?",
           "Warning",
           MessageBoxButton.OKCancel,
           MessageBoxImage.Question);

    if (result == MessageBoxResult.Cancel)
        Currency.SelectedItem = selectedItem;
    else
        selectedItem = Currency.SelectedItem;
}

Upvotes: 2

Ezra
Ezra

Reputation: 49

You can span a Task on the .Net thread pool and display a message box through a dispatcher.

Upvotes: 0

Related Questions