I frequently have the following task:
I have a collection of objects (f.e. Customers) and want to provide the user with an editor for these objects. Typically I have some list control on the left side of the editor and a form on the right side. The form displays the properties of the object that is currently selected on the left side.
Regarding the confirmation of any changes, there are at least two strategies:
make the editor a modal dialog window and give it OK/Cancel buttons. On OK save all changes for all objects
give the editor a Save button above or below the form on the right side that would allow the user to confirm changes to the currently selected object.
My question is about the second strategy, implemented as an MVVM application with WPF:
I would like to give my user a feedback that there are unsaved changes. Applications like text editors often solve this by enabling the Save button when any changes occurred and disabling it again once the user pressed it to confirm her/his changes.
If I understand correctly I would have to monitor changes to any bound properties in my form (backed by a model class). Usually my model classes use auto properties (no explicit getters and setters). Do I have to write explicit getters for all my properties to enable the Save button when anything changed, or is there a smarter way to achieve this?
Following the MVVM pattern, your ViewModels should implement INotifyPropertyChanged interface, than you can easily subscribe to PropertyChanged event and monitor properties changes
If you don't want to write INPC aware getters and setters in your model classes, then another way is to write a equality compare method instead, and then have your save command availability callback call into that to compare the "live" object with the edited one. I'm assuming you have a cloned object that is being edited in order to rollback if the user chooses not to save.
WPF will call it automatically as the user clicks around and types, or you can give it a hint with CommandManager.InvalidateRequerySuggested()
Related
I'm writing a tabbed WinForms application that contains multiple DataGridView controls which are bound to BindingSources. The BindingSources, in turn, are bound to BindingLists of business objects. My business objects implement INotifyPropertyChanged.
My TabControl has three TabPages (A, B and C). If my user has made changes to the data in the DataGridView control on tab A and then tries to switch to tab B or C without committing the changes (in this case, writing the data to disk), I need to prompt them to save the changes or lose them.
In order to determine if changes have been made on a given tab, I created "pendingChange" variables for each tab. Each DataGridView control has it's own CellValidating event handler where I validate the data as the user makes changes. At the end of that event handler, if the change has passed all of the validation checks, I set the pendingChange variable for that tab to true. When my user clicks the button to write the changes back to disk, I reset the pendingChange variable. So anytime my user attempts to change to a different tab, I check this variable to determine if there are any changes that need to be saved first.
Now I've realized that a big flaw with this design is that the CellValidating event for the DataGridView control is fired every time a cell loses focus. So now I need to find a different way to track when a cell value has been changed. Is there another event that the DataGridView control exposes that would be better suited to this purpose? Or perhaps there's an event for the BindingList that would help me achieve this functionality? If I have to, I can add a boolean "modified" property to my business object and update that as required. If it's possible to avoid that, I would like to because one of the BindingLists contains 150,000 objects at any given time and iterating through that might not be terribly fast.
BindingSource has Events. E.g. CurrentChanged event occurs when the currently bound item changes.
I have a custom user control, onto which I place a button control. I set the access modifier of the button to Public. When I drop the user control onto a form, I see the button, but am not able to select it or edit its properties in the form designer.
Ultimately, I want to create a far more complex custom wizard control, with a content panel, "Back" and "Next" buttons, etc. I have successfully created a content panel to which controls can be dropped into at design time on the main form.
However, I am bulked at not being able to edit nested controls on the user control itself.
When inheriting from a user control, or inheriting from a form, one can typically edit properties of controls whose access modifier is set to "Protected".
What do I need to do to be able to access controls of the custom user control from the designer of the form?
I think you need go to the user control designer view to modify the properties of the button (instead of the form where the user control is placed), since it is nested in the user control.
You can make basic changes to a child control of a user control on a form to the limited extent that you can expose the properties of interest via the parent user control designer and support the property changes at design time. You can make much more complex design time behaviors by writing your own custom designers but that is a potentially difficult to very difficult undertaking.
There is a good reason why it does not work the way you probably think it could and should and if you think about it carefully enough for a while, you will understand why. When you inherit from an object, yes you can change properties etc, but in this case you are creating a new type, so you can modify, add, redefine properties.
But when you drop a user control on a form, you are not creating a new type. You are creating an instance of a type, the user control, that is already defined. To the extent you can modify properties, you are modifying state that must be preserved for that instance. State must not only be persisted, but user controls also often involve painting etc so state changes can also have complex behavioral effects and this is a potentially complicated requirement that cannot be completely generalized.
So there are some modifications that are not possible at all, because they imply modifying the type, and you already have a type, you are only creating an instance. For the rest of the possible range of modifications that are ultimately state based, the platform only supports so much of the total possible state management.
I'm new to c# and I'm looking for a way to bind a property of an object of my own to the value of a textbox in a regular form (reset the property of the object everytime the value of the input changes).
I have read some information and it seems that this can be done only for database objects. Can you give me additional information.
Assuming you mean Windows Forms textbox,
say
textBox.DataBindings.Add("Text", obj, "SomeProperty");
whenever you feel like binding it. Bindings are usually done in Form_Load event handler, if the object can be obtained at that time of course, and if there's no complex logic with different data sources.
Note that this will only work in one direction (changing TextBox will yield object property changes). To sync the other way round, the object must implement INotifyPropertyChanged interface.
If you want to persist the information between runs of the application (i.e. have it be saved when you close the app and re-appear when it opens), it's easiest to use the Windows Forms designer (I assume you are coding a WinForms app) to bind the value of the TextBox to an application setting. (This article on validation provides a screenshot similar to what you want.) (EDIT: Here is the exceptional article on the subject: Exploring Secrets of Persistent Application Settings. And here is a snippet page that I put together to discuss binding.)
This binding is automatically two-way, unlike the binding that #gaearon mentions. You just need to make sure that you save the settings (i.e. Properties.Settings.Default.Save()) before closing the application (e.g. as the event handler for the Form.Closing event).
If you need more clarification, leave a comment.
I have a MVVM (Prism) application that I need to implement a master details screen wheer the master is a listview and the details is displayed next to it. Read-only seems easy enough (haven't done it yet but I've got my head around WPF binding) but edit/add confuses me.
How to I make it so the master is not updated until the details is saved?
How do I make it so you can't change the master's current selection while in edit/add mode?
I've been googling a plenty but have not found any meaty examples of this.
Thanks.
PS: This view is a child view on a larger screen. This is why I want both master and detail together.
You certainly can do this, though in my opinion such a UI design fails to harness the full power of WPF. Old WinForms UIs usually didn't update most of the application until data was saved to SQL Server (or wherever) because they didn't have real business objects and a powerful binding system like WPF. Trying to copy WinForms limitations within WPF seems like a step backward to me. Why not show the latest data everywhere it is visible in the UI, including in the master view? Also, why not allow the user to edit multiple items before saving, for example marking any edited but unsaved item with an animated marker in the master view? Combine these with a generalized undo and you have a better design and more intuitive for the user.
However if your business requirements make it absolutely necessary, here is how to do it:
Preventing changes to data from being visible outside the detail until it is saved
Upon entry into your "edit/add mode", make a copy of the data objects and set your detail view's DataContext to the copy instead of the live object. When the data is "saved", copy the data from the shadow copy back into the live object and set your detail view's DataContext back where it should be.
Preventing the master's current selection from changing while in edit/add mode
Two possibilities:
During edit/add mode, change the master view to disallow mouse hit testing or keyboard focus
When edit/add mode begins, capture the "current selection" then add an event handler that watches for "current selection" changes and immediately changes the selection back to what it was. When edit/add mode ends, remove the handler. This handler can be conveniently coded using a lambda expression and using a closure on a local variable to store the current selection.
Thanks for the answer. Now I've re-read my message, I see it is rather vague. I have a screen that edits an object which contains multiple lists of other child objects. I've implemented these as different tabs in a tab control. One of these tabs edits the comments, so I wanted to display a list of comments with an edit panel for the current selection next to the list. The user could then use add, edit or delete buttons to update the list. I wanted to do this in a pure(ish) MVVM way.
I came up with the following design which seems to work with minimal hacks.
The View includes a list of the child objects simply as a ListView bound to an observable collection within the ViewModel. I included a child object buffer – this is used to buffer changes until they are ready to be saved back to the list (or thrown away).
The View also includes an edit panel bound to the buffer object in the ViewModel. The buffer is updated whenever the list view’s current selection changes using a deep copy. I tried using data binding on the Selecteditem property but the set was never called, so a small code-behind method was added to force the property to be updated when the selection was changed.
The list view and edit view are mutually exclusive. In theory you could hide the disabled one, perhaps using a flip screen. As a general pattern, it is better for my app to have both visible at the same time as the edit panel may show extra information not shown in the list view. The choice as to which panel is enabled is controlled by binding IsEnabled to a ViewModel property like IsEditCommentMode.
Commands to manage the list have to be added, these are New, Editand Delete. Note that Add and Edit will set set up the buffer then set IsEditCommentMode to true. These list management commands are only available when IsEditCommentMode is false.
The edit panel implements Save and Cancel commands, they are only be enabled when IsEditCommentMode is true. When Save is executed, it should copy from the buffer to the list (either add or update) and fire the change notification. Finally, it should set IsEditCommentMode to false.
This all works well and does not seem to violate any MVVM tenents (in my humble but often flawed opinion).
I have two tabitems. User will enter some data and save it on the first tab. The second tab lists the saved data. What I need is when the user select the second tab before saving data in first tab a confirmation message box with Yes, No and Cancel should be shown. If the user clicks Yes button the data should be saved and go to the second tab. If he hits No, the data need not to be saved and finally if Cancel is hit the tab will retain with all entered data. How can i make this?
To keep things simple you can do the follwing in the Code Behind file.
I'd create a Model class of the data you want to display and edit in the WPF Control. Make the Model implement the INotifyPropertyChanged and IEditableObject interfaces.
INotifyPropertyChanged will allow you to Bind to the Model.
IEditableObject will allow you to provide Edit, Save and Cancel functionality.
The TabControl has a SelectionChanged Event you can handle, that will allow you to detect when the user changes 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 you can use to detirmine what button the user clicked and perform the appropiate action.
This is not a geat way to do things, but it keeps things simple, you may want to look into some WPF design Patterns to help with Code Manageability.
If you need anything explained further, just ask.
Although I disagree with the way you interrupt the user's flow from tab to tab I'm going to humor you and answer the question:
You'll need two things to get this done:
The event that occurs when a tab was clicked
The previous tab that was selected (the one you came from)
The first item:
The tab control has a Click method that you can subscribe to:
Click=”MyTabButton_Click”
The second item:
This part you'll have to do manually. You can set a variable in the click event which contains what tab was last selected. Once this is set you can check a variable (which you previously set) as to what tab was previously selected. You can then do all your validation.
Delphi's TPageControl has an OnChanging event with an "AllowChange" parameter. I guess there is something similar in WPF.