Getting RaiseFocusEvent to work

Sep 22, 2010 at 5:06 PM

I was trying this feature out, set my textbox's xaml as the article suggests, and under my viewmodel's ViewAwareStatusServiceViewLoaded, a RaiseFocusEvent("Name") that the text box is bound to. However, when the viewmodel runs, it causes a NullReferenceException on FocusRequested(focusProperty) in ViewModelBaseWPFSpecific. I have a feeling I'm missing something obvious, but my brain isn't fully MVVM\Cinch certified yet :D

Sep 23, 2010 at 3:20 PM

I cant really comment on this, quite hard to visualise what you issue is from this bit of text, but it obviously worked just fine for both my SL and WPF demo apps.

Sep 23, 2010 at 6:29 PM

OK then...

Is there a restriction to when I can call RaiseFocusEvent in my viewmodel?


I added

   <CinchV2:TextBoxFocusBehavior IsUsingDataWrappers="false" />

to my textbox that was bound to the Name property.

In ViewAwareStatusService_ViewLoaded in my viewmodel, I added RaiseFocusEvent(Name)

When the viewmodel is loaded, this causes a NullReferenceException in FocusRequested(focusProperty), specifically FocusRequested == null.

In the WPF Cinch v2 Demo, the example used for RaiseFocusEvent occurs in a normal method which obviously works, so I wondering if I've done something wrong by trying to use it in ViewLoaded event handler.

I should also mention I'm using only VS2010, haven't really grokked Blend yet...

Sep 24, 2010 at 8:38 AM

Could be something to do with it being done in OnLoaded, Ill have a look when I find some time, which sorry to say, may take a while, technically reviewing book right now.

Sep 29, 2010 at 4:23 AM

It's very low priority thing :D

Oh, and while I'm here, thanks for your all your work on Cinch. Looking at my code from before I discovered MVVM and cinch makes me cry....

Sep 29, 2010 at 8:57 AM

I hope it makes you cry with happyness.


Hey I thought of another way to fix your problem, so on your view have it implement some interface, like IFocusable or something, where IFocusable looks like this


public interface IFocusable


Focus(string nameOfElement)


Then in your ViewModel you can grab the View using that interface using the ViewAwareStatusService something like this:

IFocusable focusable =  this.ViewAwareStatusService.View as IFocusable;

focusable.Focus(name of element you want to focus);


Then in the View that implements IFocusable, when the Focus() method is called, you will need to find the element requested (this.FindName or some VisualTree thing) and Focus it.



Feb 13, 2013 at 4:12 PM
Hi Sacha,

I know this thread is quite old but I have recently started using your fantastic Cinch framework and although your proposed solution will probably work fine (IFocusable thing), I wrote an extension method for the ViewModelBase using a Timer to "wait" for the FocusRequested to be available:
public static class ViewModelBaseExtensions
    public static void RaiseFocusEvent(this ViewModelBase viewModelBase, IViewAwareStatus viewAwareStatusService, string focusProperty)
        var timer = new Timer();

        timer.Elapsed += (source, e) =>
            viewAwareStatusService.ViewsDispatcher.InvokeInBackgroundIfRequired(() =>

This works fine and I use it in a regular ViewModelBase-derived class:
public void InitialiseViewAwareService(IViewAwareStatus viewAwareStatusService)
    this.ViewAwareStatusService = viewAwareStatusService;
    this.ViewAwareStatusService.ViewLoaded += ViewLoaded;

private void ViewLoaded()
    this.RaiseFocusEvent(this.ViewAwareStatusService, "PropertyName");
Delayed execution is often seen as bad design and I would appreciate your opinion in this case.

Thank you,
Feb 20, 2013 at 4:04 PM
Sorry I have been on holiday. To be honest if that is what your need is, I see nothing wrong with what you have done.

That's really the only advise I can give you.
Feb 20, 2013 at 8:22 PM
No problem!

Thanks for replying.