How do I keep bindings from firing TextChanged event? - c#

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.

Related

Don't have Click event in TextBox in WPF C#

I just want to add simple thing, wherever i click on TextBox I want to delete text that is inside. The thing is I DON'T have in list of events, event called Click.. Is this even possible? Or just I need to install some add on. Buttons are fine, they have Click event.
MouseButtonLeftUp would work if you only are concerned with mouse clicks. With that said, what if someone tabs into the box or focus is entered by other means?
At that point you may want to look at the GotFocus event. Any time the TextBox receives focus, you can handle the event.
TextBox has events called MouseLeftButtonUp and MouseLeftButtonDown, you can use them.

WPF: Checking if user has changed one of the controls

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.

Create selection change event in custom control using dependency property

I have used a custom control in wpf from Source code for multiselect. that control is a multiselect combobox with checkbox. but selection changed event is not present in that.I am trying to create a selection changed event using DependencyProperty. I am new to WPF. I don't know how to use DependencyProperty for creating events. Can Anyone tell me how to create selection change event in that control.
Combobox image is here
You need to use a RoutedEvent.
Create a custom routed event
But better yet you can use an existing routed event from the selector class called SelectionChanged
Raising the selection changed event from your code: psedo code since i am not near vs.
SelectionChangedEventArgs args = new SelectionChangedEventArgs();
args.AddedItem.add(... how ever is checked ..);
args.RemovedItem.add(... how ever was unchecked ..);
this.RaiseEvent(args); // this your control.

How can I get an event to be raised every time any control is changed?

I have a long form with a bunch of CheckBox's and occasionally some TextBox inputs. I want an event to be raised any time any control is changed (i.e., any CheckBox state is changed or any TextBox.Text is altered). Is there a global way to do this without having to add an event handler to each and every control?
One of the advantages of WPF and its declarative nature is that events are inherited down the visual tree.
<Window x:Class="MyApplication.MainWindow"
....
TextBox.TextChanged="TextBox_TextChanged" CheckBox.Checked="CheckBox_Checked">
All TextBox and CheckBox controls will inherit these event handlers. The same approach can be taken in other controls such as Grid so only the controls within the Grid are affected.
One way do this would be to subclass the CheckBox and TextBox classes and implement the required handlers in those classes.
You will still need to go through your application and replace the standard CheckBox and TextBox with your classes.
You can use TextBoxBase.TextChanged="MainWindow_OnTextChanged" on window or user control.
There are so called class events in WPF. Put this in the form's constructor:
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.TextChangedEvent,
new RoutedEventHandler(AnyTextBox_OnTextChanged));
This way you register a handler to TextChanged event of all text boxes. Similarly with check boxes:
EventManager.RegisterClassHandler(typeof(CheckBox), CheckBox.CheckedEvent,
new RoutedEventHandler(AnyCheckBox_OnChecked));

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.

Categories

Resources