I have a Window with many items in it, is there a Window or Grid Event to tell if the user has changed any textbox, combobox, radiobutton, checkbutton etc? I don't want to go through each item and add SelectionChanged event as this is just to tell if anything has changed since the data was last saved.
I think you should implement INotifyPropertyChanged interface which notify you once any property changed.
Here is an example that describes "Bind Better With INotifyProperty". This example is for a Windows App, but hope it would give you an idea.
Doing your job in this way is much elegant than adding events for each controls.
Related
I have a WPF window with about 30 controls on it (text and comboboxes). There is no binding, I just populate them from a dictionary in Loaded event. I need to be able to know if user has changed any of them. Is there a way to do it other than setting a flag in 30 "Changed" events?
I tried to research it and saw a lot of info on implementing IsDirty property but I am not sure if it is applicable to my simple unbound window.
You can use one changed event handler that is attached to all 30 events.
I am attempting to choose a specific item upon the SelectionChanged event of the ListPicker for WP8.I have two list pickers, I want to implement them such that when I select a particular item it changes the items on the other ListPicker. Any ideas how I could achieve this thanks in advance?
If you want to stay within the mvvm model, it might not be a bad idea to implement a attached property to invoke a command when the selection changes have a look at this post which shows an implementation:
ListBox SelectionChanged not getting called
I'm new with MVVM and I'm stuck...
I have a ListBox in the MainWindow. The ListBox contains Items of type WhatEverViewModel which are displayed by DataTemplates. The user can interact with these items and the WhatEverViewModel has several DependencyProperties which may change during interaction.
The question I have is:
How can I elegantly react (in the MainWindowViewModel) to changes of DependencyProperties of the CURRENTLY SELECTED WhatEverViewModel. I personally would implement some events in the WhatEverViewModel and when the SelectedItem of the ListBox change, I would attach to the events of the currently selected WhatEverViewModel. But I think in MVVM there might be a more elegant way to solve this...
Thank you.
Make CurrentWhatEver a property of your MainWindowViewModel and bind the Listbox.SelectedItem property on it. This way, MainWindowViewModel knows when the selected WhatEver changes and can register/unregister to events it's interested in.
communication between viewmodel can be done in several ways.
Messenger/Mediator like the one form MVVM Light
Eventstuff like the one from PRISM
or simply use harcoupling and subscribe to the events from the WhatEverViewModel in your mainviewmodel.
btw why in hell to you use DependencyProperties in your Viewmodels? simple Properties with INotifyPropertyChanged are the way to go.
one more thing. why you want to react to changes in the SelectedViewmodel(or better what you want to achieve, with the selected viewmodel.)? if you just want to display some information in your view, simply bind the SelectedViewmodel to it. you should specify your question in that way.
EDIT
The WhatEverViewModel has a list which is also bound to a listbox (in
datatemplate) and depending on what I select in the WhatEverViewModel
I want to display some kind of "Configurator" in the MainViewModel. –
JensPfister1 1 hour ago
why not simply bind the SelectedWhatEverViewmodel.SelectedListEntryProperty to your configurator view? can you post some code?
You should implement the INotifyPropertyChanged interface on each of your ViewModels. Then when one of your properties changes call the PropertyChanged event, your views will get notifications that the property has changed (as long as your Binding is correct). If a property is a list or collection make sure that the list is based off an INotifyCollectionChanged.
Add a property for the Selected WhatEverViewModel to your MainWindowViewModel, bind that in your ListBox. Then in your MainWindowViewModel you can hook on to the property changes of your Selected WhatEverViewModel.
For more guidance read:
WPF MVVM INotifyPropertyChanged Implementation - Model or ViewModel
http://blogs.msdn.com/b/soultech/archive/2011/06/07/mvvm-tips-when-implementing-inotifypropertychanged.aspx
I have my logic of opening a search modal window based on user selection in a combobox. What happens is the modal window opens but the selected item is not reflected in the combobox. i.e Unlike a Windows form, the WPF combobox is not reflecting the newly selected item when the selectionchanged event is fired.
well i would have used the SelectionChangeCommitted in a Winform, i am not finding an equivalent in this case. Am i missing something thats obvious? Thanks!
Are you opening the modal window in the context of the selecteditemchanged event of the combo box? I would recommend opening the modal window using the dispatcher. This will give the bindings time to catch up if that is the issue. Are you binding the selected item of the combo box to a property on a model / view model? If so is this a one way or two way bind?
It would help if you posted some code along with your question.
After experiencing it myself, I did a quick search and found that SelectionChanged will bubble from a ComboBox to a parent TabControl if left unhandled in:
In C# WPF, why is my TabControl's SelectionChanged event firing too often?
My question is why? What is the reasoning behind this? I feel like I'm missing something pretty important about WPF and events.
Most events in WPF will bubble (or tunnel) until someone sets Handled=true on the event args. The upside of this is that suppose you had multiple comboboxes within a single tab control - you could in a single place handle changes to all of those boxes. You can do this in addition to handling the event seperately on each ComboBox or also handling a consolidated event even higher up in the tree like monitoring all ComboBoxes within the entire window.
This is what WPF calls "routed events". For a good intro to the topic, check out http://msdn.microsoft.com/en-us/library/ms742806.aspx