Confirmation when switching between tabs
I have two tablets. The user enters some data and saves it in the first tab. The second tab displays the saved data. I need when the user selects the second tab before saving the data on the first tab, a box with the message "Yes", "No" and "Cancel" should be displayed. If the user clicks "Yes", the data should be saved and go to the second tab. If he clicks No, the data does not need to be saved, and finally, if Cancel hits, the tab will save all the data entered. How can i do this?
a source to share
To keep things simple, you can do the following in your Code Behind file.
I would create a data model class that you want to display and edit in WPF Control. Inject Model into interfaces INotifyPropertyChanged
and IEditableObject
.
INotifyPropertyChanged
will allow you to bind to the model.
IEditableObject
will allow you to provide editing, save and undo functionality.
There is an event in the TabControl SelectionChanged
that you can handle that will allow you to detect when the user is changing tabs, in this handler you can use System.Windows.MessageBox
to ask the user to save, etc., System.Windows.MessageBox.Show()
returns a MessageBoxResult
Object that you can use to determine which the user clicked the button and take the appropriate action.
This is not something to do, but it simplifies the process, you can look at some WPF design patterns to help with code manageability.
If something is explained to you, just ask.
a source to share
While I disagree with how you interrupt the flow of users from tab to tab, I'm going to tell you about it and answer the question:
To do this, you need two things:
- Event that occurs when clicking on a tab
- The previous tab that was selected (the one you came from)
First element:
The tab control has a Click method that you can subscribe to:
Click="MyTabButton_Click"
Second element:
You will have to do this part manually. You can set a variable on the click event that contains the last tab. After that, you can check the variable (which you previously set) against which tab was previously selected. Then you can do all your validation.
a source to share