<p>I am following the <a href="http://blogs.msdn.com/johngossman/archive/2005/10/08/478683.aspx" rel="noreferrer">M-V-VM</a> pattern for my WPF UI. I would like to hook up a command to the TextChanged event of a TextBox to a command that is in my ViewModel class. The only way I can conceive of completing this task is to inherit from the TextBox control, and implement ICommandSource. I can then instruct the command to be fired from the TextChanged event. This seems to be too much work for something which appears to be so simple.</p>

<p>Is there an easier way (than subclassing the TextBox and implementing ICommandSource) to hook up the TextChanged event to my ViewModel class?</p>

[{'answer_id': 56081, 'author': 'Kent Boogaart', 'author_id': 5380, 'author_profile': 'https://Stackoverflow.com/users/5380', 'pm_score': 2, 'selected': False, 'text': "<p>Can you not just handle the TextChanged event and execute the command from there?</p>\n\n<pre><code>private void _textBox_TextChanged(object sender, EventArgs e)\n{\n    MyCommand.Execute(null);\n}\n</code></pre>\n\n<p>The alternative, as you say, is to create a <code>TextBox</code> that acts as a command source, but that does seem like overkill unless it's something you're planning on sharing and leveraging in many places.</p>\n"}, {'answer_id': 56164, 'author': 'Samuel Jack', 'author_id': 1727, 'author_profile': 'https://Stackoverflow.com/users/1727', 'pm_score': 5, 'selected': True, 'text': '<p>First off, you\'ve surely considered two-way data binding to your viewmodel, with an UpdateSourceTrigger of PropertyChanged? That way the property setter of the property you bind to will be called every time the text is changed?</p>\n\n<p>If that\'s not enough, then I would tackle this problem using Attached Behaviours. On Julian Dominguez’s Blog you\'ll find an <a href="http://blogs.southworks.net/jdominguez/2008/08/icommand-for-silverlight-with-attached-behaviors/" rel="noreferrer">article</a> about how to do something very similar in Silverlight, which should be easily adaptable to WPF.</p>\n\n<p>Basically, in a static class (called, say TextBoxBehaviours) you define an Attached Property called (perhaps) TextChangedCommand of type ICommand. Hook up an OnPropertyChanged handler for that property, and within the handler, check that the property is being set on a TextBox; if it is, add a handler to the TextChanged event on the textbox that will call the command specified in the property.</p>\n\n<p>Then, assuming your viewmodel has been assigned to the DataContext of your View, you would use it like:</p>\n\n<pre><code>&lt;TextBox\n  x:Name="MyTextBox"\n  TextBoxBehaviours.TextChangedCommand="{Binding ViewModelTextChangedCommand}" /&gt;\n</code></pre>\n'}, {'answer_id': 56191, 'author': 'Nidonocu', 'author_id': 483, 'author_profile': 'https://Stackoverflow.com/users/483', 'pm_score': 3, 'selected': False, 'text': '<p>Using the event binding and command method might not be the right thing to use.\nWhat exactly will this command do?</p>\n\n<p>You might want to consider using a Databinding to a string field in your VM. This way you can make a call to a command or function from there rather than having the UI care at all.</p>\n\n<pre><code>&lt;TextBox Text="{Binding WorldName}"/&gt;\n....\npublic string WorldName\n{\n    get\n    {\n        return WorldData.Name;\n    }\n    set\n    {\n        WorldData.Name = value;\n        OnPropertyChanged("WorldName");\n        // CallYourCustomFunctionHere();\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/55855', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/708/']