Hi i am trying if it's possible having an Event like TextChanged (of a TextBox) located in another place independent of the Window CodeBehind (like a Class).
What i am trying to do is having in the ResourceDictionary a reference to an event of the TextBox. (Because the ResourcesDictionaries doesn't have a CodeBehind.)
Note: It really have to be by this way, because i am customizing another control to have some TextBoxes dynamically and all the TextBoxes will fire the same event when TextChanged occurs.
Edit: Disregard this unless you want to subclass TextBox to execute a command on TextChanged. (I think the trigger-method is to be preferred.)
I would suggest Commands for this; define a RoutedCommand somewhere:
Set it to the button in the dictionary:
And create a command binding:
I would suggest triggers. You can read more about them here: http://www.silverlightshow.net/items/Behaviors-and-Triggers-in-Silverlight-3.aspx
Essentially you can, in xaml, hook a control's event to a trigger and have it execute a method or command else where.
Edit: I should note that while it does say 'Silverlight' it all applies to WPF as well. Edit2: The MVVM toolkit provides an EventToCommandTrigger that would let you do something like this:
http://blog.galasoft.ch/archive/2009/11/05/mvvm-light-toolkit-v3-alpha-2-eventtocommand-behavior.aspx
Kudos to H.B.'s answer for reminding me about the command way of doing this
Edit3: Better example, this borrows heavily from how the MVVM world would do this. Since the EventToCommand binding would attached to whatever the control's context is, you could stick this in your ResourceDictionary and anywhere you place it, it would attempt to find that TextChangeCommand property.
and the codebehind: