[Example 4 - Introducing Behaviors and Dependency Properties]

With our previous example, we saw that our user input was successfully being evaluated when we clicked the "Is it a Palindrome?" button.  Now we want to trigger the same command when the "Enter" key is pressed within the textbox.  But how can we do this in a reusable fashion, without requiring modifying the codebehind?

The answer is with a custom behavior.  Blend already knows about certain built-in behaviors, and you can also register custom behaviors so that they can be associated with XAML controls in the designer.

MvvmPalindrome.csproj
 * We first add a reference to the Behaviors SDK.  In visual studio, this can be added by right-clicking the "References" node in solution explorer, then clicking "Add Reference".   From there, you expand "Windows" and select "Extensions" and finally check "Behaviors SDK (XAML)" and click "Ok."

CommandOnEnterKeyBehavior.cs:
 * This is a completely new file, and defines our custom behavior.  Behaviors must be a DependencyObject, and implement the IBehavior interface.
 * We decorate our custom behavior class with a TypeConstraint of TextBox, which indicates that it is only valid to attach this behavior to a TextBox.
 * The IBehavior interface specifies 3 members: "AssociatedObject", "Attach", and "Detach"
 * The "AssociatedObject" tells us what control this behavior is attached to.
 * The "Attach" method allows us to register for any events we are interested in.  In this case, we are only interested in the KeyDown event.
 * The "Detach" method allows us to cleanup any registrations if the behavior gets detached from the control.
 * Note that for this example, we assume that the AssociatedObject will never be null.  This may not always be a valid assumption depending on how the behaviors are used.
 * Finally, our behavior requires (for our purposes) an additional Dependency Property - "Command", which allows us to bind the command to be executed in XAML. We currently ignore the case where a command takes a parameter, but we could easily implement that with an additional CommandParameter dependency property.  Dependency properties have special behavior that is managed by the runtime that allows binding, styling, and other functionality.

MainPage.xaml
 * Here we import two namespaces.  This is done in xaml by defining an xmlns prefix.  The prefix can be anything we want, so long as it is unique in the XAML file and is valid xml.  The "Interactivity" namespace is part of the Behaviors SDK.  We also import the namespace for our custom behavior.
 * Inside of the TextBox element, we first add the Interaction.Behaviors tag.  Inside of that, add the tag for our custom behavior.  Notice how our "Command" dependency property defined in the behavior class can be bound in exactly the same way as for the built-in Button element.