Silverlight Focus Action

FocusAction with Silverlight

Download action:

Download demo project: MVVMFocusTextbox

When I use MVVM in my projects I try to avoid writing code in the code behind (.xaml.cs). Although I DO think it’s valid to write User-Interface-specific code in the code behind, I’m always looking for a code-behind-less solution.


Setting a textbox focus in a MVVM solution is done from the code behind.


Use a FocusAction on a TextBox that is triggered from your ViewModel. Download and add it to your project. Drop the FocusAction on a User Element and set the folowing properties:


In xaml it looks like:

    <i:EventTrigger SourceObject=”{Binding Mode=OneWay}” EventName=”NewTask”>
        <Focus:FocusAction TargetObject=”{Binding ElementName=textBox, Mode=OneWay}” TargetName=”textBox”/>

Of course you may use any other event like an ItemChanged event to trigger the FocusAction.

Written by Loek van den Ouweland on May 15, 2010. Questions regarding this artice? You can send them to the address below.
By using this site, you acknowledge that you have read and understand our Cookie and Privacy Policy.