Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

wpf

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?

like image 932
Doug Avatar asked Dec 26 '22 21:12

Doug


1 Answers

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.

like image 86
keyboardP Avatar answered Feb 22 '23 23:02

keyboardP