Reputation: 4562
In my application I have created a custom messagebox(with a different backgroud color) using a Popup. There it has a button and I have implemented the button click event.
With in the button click, it has a message box and according to "Ok" or "Cancel" it performs the operation from there onwards.
The problem is after I click the button in the popup(custom messagebox), the message box loads, but still the popup(custom messegebox) appear in the background. I need the custom popup to close entirely and to perform operations. Therefore I have used popup.IsOpen = false;
as the first statement within button click, but still it appears in the background untill it finish the entire button click event. I searched for other properties, but didn't find any working.
following is the code
Popup popup = new Popup();
popup.Height = 300;
popup.Width = 480;
popup.VerticalOffset = 100;
CustomMessageBox control = new CustomMessageBox();
popup.Child = control;
popup.IsOpen = true;
this.IsEnabled = false;
control.OK_BTN.Click += (s, args) =>
{
popup.IsOpen = false;
this.IsEnabled = true;
MessageBoxResult result = MessageBox.Show("Do you want to reset the settings ?", "Settings", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
changeSettings();
}
};
Any suggestion to achieve this is highly appreciated. Thanks...!!!
Upvotes: 1
Views: 1236
Reputation: 985
I was just typing what Negative Eddy wrote.
Using Dispatcher.BeginInvoke will work! I tested it, works perfectly. I don't have enough rep to comment on his apparently.
Upvotes: 1
Reputation: 401
The reason this happens is because all of this is happening on the UI thread which is getting blocked until you return. Until your button handler returns, no updates to the UI will happen.
If you really want the popup to disappear first, you would need to do something like
control.OK_BTN.Click += (s, args) =>
{
popup.IsOpen = false;
this.IsEnabled = true;
Dispatcher.BeginInvoke(() =>
{
MessageBoxResult result = MessageBox.Show("Do you want to reset the settings ?", "Settings", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
changeSettings();
}
});
};
That would force the MessageBox to be opened after your button handler returns. But are you sure you want the popup to disappear first? What if the user clicks cancel on the message box? Typically the message box appears directly on top of whatever invoked it and doesn't replace it.
Upvotes: 2