Silverlight Focus Action

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.

Grüsse aus Berlin!
This is a cookie free and popup free website.
Join me in making the web enjoyable again by removing the need for annoying cookie warnings and silly newsletter overlays!