WPF: Checking if user has changed one of the controls - c#

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.

Related

How to see all TriggeredEvents for control

Anyway to view all the events triggered specifically for a ui control.
I have integrated WPF toolkit extended into my application and currently facing an issue where the TimePicker does not trigger the ValueChanged unless it loses focus. that would be understandable but it has buttons in the control that changes the value ... so losing focus is not ideal as I would require to click on another control.
This is the first time I get this type of issue but there is a lot of times where I am not too positive which event I want to use so I just put a bunch of events with breakpoints and see which one gets hit with the ideal moment.
For that reason, I am curious to know if there is any sort of debugger tool or something similar that can register/show all the events being hit without me putting a breakpoint in every event or modifying the controls code ?
If ValueChanged is a RoutedEvent you could use a tool like snoop. http://snoopwpf.codeplex.com/
the events tab will show you events as they are triggered (note that you'll have to check off the event that you want to listen to in the dropdown.

Why does SelectionChanged bubble from ComboBox to parent TabControl?

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

WPF SelectionChanged event for all items in window?

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.

How do I keep bindings from firing TextChanged event?

I'm using the Control.TextChanged event to detect when the user has modified the form. I have a method that loops through every control and adds the same TextChanged to all the controls.
My problem is, on the form I also have databinding that binds bindings that have Binding.Format() and Binding.Parse(), and these are within a TabControl. If the user changes tabs (SelectedIndexChanged), it then activates the Format/Parse and fires the TextChanged event which makes it seem like the form's been modified!
How can I either (1) keep the bindings or Binding.Format and .Parse from firing the TextChanged event, or (2) implement a better way to detect of the controls on the form have been modified?
Instead of checking the actual TextChanged Event, check the backing properties and look at when they change. You can make use of INotifyPropertyChanged to help out with this.

Is there a way to detect if the user is editing a property in a property grid?

I have a Windows form (.NET 3.5) that contains a propertygrid control. The propertygrid control gets refreshed periodically do display any changes that may have occurred in the class which it represents. I want the refresh to only occur if the user is not currently editing a property in the grid. Is there a way to detect if the user is currently editing a control?
Yes - it's a little hacky but you can find out which subcontrol of the property grid is active, and make an educated guess based on what it is. The following seems to work:
bool isEditing = (propertyGrid.ActiveControl.GetType().Name != "PropertyGridView");
There probably is, but might I recommend having your type implement INotifyPropertyChanged instead of refreshing the grid on a timer? This way you would never have to call Refresh yourself; the display would automatically update the value displayed for each property whenever that property changed.
Of course, if your type has tons of properties, or if you're using your grid to dynamically display objects of many different types, this suggestion may not be practical. It's just a thought.
This is a fairly complex problem. I'd suggest a two fold approach:
Keep track of the last time the changed events fire.
Keep track of whether or not the control has focus.
If the control hasn't been modified within a certain threshold and has focus, or if the control doesn't have focus, I'd consider that to be sufficient to determine that it is not currently being edited.
You could hook up the OnLostFocus event. This way, the control would only get updated once it no longer had focus.
protected virtual void OnLostFocus( EventArgs e)

Categories

Resources