In my CustomerCollectionViewModel, I have a ObservableCollection<CustomerViewModel> CustomerList and a property called CurrentCount property that returns CustomerList.Count().
In the associate view, the I bind CustomerList to a DataGrid and CurrentCount to a label. In reality their are many other dependent properties that present aggregate information about the the CustomerList. About a dozen or so.
My current understanding of MVVM would require bloating the CustomerViewModel with SendPropertyChanged() for all the aggregate summery labels in UI. Isn't it possible to all CustomerViewModel update events that bubble out of the DataGrid be caught and then fan out to update the aggregate labels.
I bound the lost focus event of a textbox style to a command in the immediate viewmodel's data context. This command takes the textbox name as the parameter. This was achieved through using MVVM-Light's relay command, but there are other ways of doing this.
Then in my viewmodel I have a LostFocusCommand that I use to "refresh" my aggregate values and notify the UI of the changes.
All I have to to is apply the text box style to the textboxes i want to trigger this command event (i have a style for comboboxes etc. that binds to the same command, so It does not just have to be text boxes.).
One place to maintain code and you can evaluate the textbox name to take more specific or isolated actions.
Hope this helps. can't be more specific without seeing code or knowing what framework your using.
Related
I have heard a lot about two-way bindings in WPF, but I'm not entirely clear on how to accomplish it or what it actually means.
I have a ListView with a bunch of items in it. When the user selects a new item, a TextBox in the application will change its text to display some property of the selected item.
But when the user changes the text in the text box I want the ListView item to be updated immediately as well. Is there any "two-way binding" magical WPF way of accomplishing this?
Mark's answer shows how to accomplish what you want, but you also asked more generally about "how to accomplish [two-way binding] and what it actually means."
One-way binding means that the binding target (e.g. control) will display data from the binding source (e.g. business object), and will update itself as the business object changes, but that changes to the control will not be propagated back to the business object. E.g. if the Person.Name changes from "bob" to "kate", the TextBlock.Text bound to the Name will change from "bob" to "kate" too.
Two-way binding simply means that not only are changes in the business object reflected in the UI, but changes made by the user in the UI are propagated back to the business object too. So now when the user edits the TextBox.Text bound to the Name, say changing "kate" to "edmund", WPF will set the Person.Name property to "edmund" as well.
To accomplish this, just set Mode=TwoWay on the Binding declaration. Some properties bind two-way by default: TextBox.Text, for example, binds TwoWay by default, which is why Mark's code doesn't need the Mode declaration. In addition, as Mark notes, by default WPF only propagates changes back to the business object when the control loses focus. If you have two UI elements bound to the same property, this can mean they appear out of sync, in which case you can use the UpdateSourceTrigger to force WPF to propagate whenever the property changes.
MSDN covers this in detail with some good clear diagrams: see Data Binding Overview in the WPF SDK.
If you haven't you'll need to implement INotifyPropertyChanged for your class that you're binding to.
Also, when you say you want the ListBox item to be updated immediately, you mean that you want it to change as you type in the TextBox. By default the TextBox.Text property updates its source when it loses focus, but you can change this by setting the binding UpdateSourceTrigger to PropertyChanged:
{Binding Source={...}, Path=Whatever, UpdateSourceTrigger=PropertyChanged}
What is the Type of the items in the ListView? To get the two way binding going the need you implement INotifyPropertyChanged...
This might help WPF event property changed?
How is this possible for WinRT?
I have read the answer for WPF How to programmatically click a button in WPF?, but does not really help me.
Anyone solved such an issue?
First, I'd like to say if you want the button to "appear to have been pressed (in terms of animation/highlight effects) this won't help you but otherwise it should.
My Advice to you would be to follow the Model-View-ViewModel (MVVM) design pattern when designing your application if you haven't already. That way instead of calling the "button" click you can simply execute the method in your viewmodel that would normally be bound to that click.
Example:
You create a model class representing data in your database.
You create a view (page/window) with buttons and other UI elements on it.
You create a ViewModel class that has a series of public methods and collections.
Now in the XAML for the View, you bind the ViewModel as your DataContext and bind the public properties of the ViewModel to your collections (ItemSource for a ListBox being bound to an ObservableCollection is on example). You can create public methods that are "commands" and bind them your buttons so that when the button click event is fired, the command in the view model is executed. Now for all your unit tests and for any other reason you might want to programmatically "click" the button, you can simply call the associated methods in the ViewModel and never worry about what the actual View is doing.
I am having a form with different type of controls like Text Box, Drop downs, Check box, Radio buttons etc. All these controls are loaded dynamically from database at run time.
I want to perform validation on Text box on conditional basis. For example, If we have selected any value in drop down, then you must have to fill details in Text box. Otherwise text box details are not required.
I am open to use database to perform this task and I am using MVVM pattern in my project.
Any help on this is highly appreciated.
Thanks.
(I started this as a comment, but it ended up being too long).
In theory you have access to all these controls and their values in your ViewModel.
Without knowing the specifics of your program, it's difficult to suggest anything useful, but in essence you need to expose some more properties from your ViewModel (probably boolean) which will be calculated based on the values in your controls. Then you need to bind IsEnabled properties on your controls to these new properties.
Sounds simple, but I think you have some architectural problems which will make it difficult to implement what I suggested above. In order for this to work and automatically update your controls whenever other controls' content change, your ViewModel needs to implement INotifyPropertyChanged and raise PropertyChanged event every time you update one of those boolean properties.
I think what you're trying to do could be achieved with ItemsControl and DataTemplates (and maybe DataTemplateSelectors). This will allow you to store "data" in your ViewModel (say List or something more specific) without referencing the actual Controls and the relevant DataTemplates will add the right controls for different data types you have in your ViewModel.
I am in the process of re-writing one of our large Silverlight apps to use PRISM and the MVVM design pattern.
A very common scenario is a DataGrid in the View. Double clicking a row allows the user to edit the entity represented by the row, using a ChildWindow.
I am tempted just to capture the DoubleClick event in the code behind, create a new ChildWindow of the proper type, and set the DataContext to be DataGrid.SelectedItem.
I know that this is not the proper way to handle this scenario with PRISM and MVVM, however.
I would love advice on what is! (re: my title...it seems like InteractionRequest might be the best way to do this?)
Thanks...
EDIT: We did end up deciding to go with InteractionRequest for our solution. We almost always use "Notification" as the type and pass a new ViewModel (each ChildWindow has its own) as the Content.
In our case the ChildWindow view was complex enough to warrant its own viewmodel. This view isn't too closely coupled with the data grid view.
So, we have an EventTrigger attached to the data grid (we actually use Telerik's data grid) in XAML. The event trigger executes a command in the view model using InvokeCommandAction.
The command publishes an aggregated event that has the selected item as the payload. The event is picked up by the central application controller that is responsible for creating the ChildWindow view and a corresponding view model (using the event payload as the context).
I think that interaction request could potentially be used in your case, but based on my understanding the idea behind an interaction request is a very simple Ok or Yes/No interaction. You might be pushing the boundaries with a bunch of text boxes, validation, etc.
No database involved here, I am creating my own datatable in code, and binding it to a textbox and a datagridview.
When I change current record in the grid the textbox updates to the current value.
But what's the best way to synchronise changes between the values in the textbox and the datagrid. If I change one then it doesn't change the other unless I go to another record and back.
I can do this by adding a datagrid.Refresh() to the textbox Validated event, and presumably something to the CellValidated event on the datagrid, but it seems like I might be going about this the wrong way.
Edit:
Based on the answer below, my question should be: is there a way to notify bound controls of changes in a DataTable they are bound to, or must the code use a BindingList or do it manually instead.
I suggest you use a BindingList<T> rather than a DataTable, where T is your "business object" that represents each record displayed in the grid. Then your business object should implement INotifyPropertyChanged and fire NotifyPropertyChanged whenever the value in the text box changes, either by binding the desired property to TextBox.Text or updating the appropriate property of the selected business object whenever TextBox.TextChanged fires.