Ross McCulloch
Ross McCulloch

Reputation: 107

Unable to dynamically set focus in WPF MVVM application

I'm working on a WPF application using the MVVM pattern, and I have hit a problem with focus.

I have full screen slide on 'overlays' and within each overlay I have a ContentPresenter that I use to display arbitrary view models/views by data binding it's content to a view model property I set in my data context, like so:

<Grid Name="OverlayContainer"
      FocusManager.IsFocusScope="True"
      KeyboardNavigation.TabNavigation="Cycle"
      IsEnabled="False">

  <Grid.RenderTransform>
    <TranslateTransform x:Name="OverlayContainerTransform"
                        X="{Binding ElementName=OverlayContainer, Path=ActualWidth}" 
                        Y="0"/>
  </Grid.RenderTransform>

  <ContentPresenter x:Name="OverlayContent" Content="{Binding Path=OverlayViewModel"/>

</Grid>

I can then dynamically set the OverlayViewModel property to various view models when needed and use data templates to get WPF to automatically display the correct view for the relevant view model:

<DataTemplate DataType="{x:Type ViewModels:AuthorisatonViewModel}">
    <Views:AuthorisatonView/>
</DataTemplate>

When I change my view model and slide on the overlay I set focus scope to the overlay focus scope (which I seem to be able to do OK with OverlayContainer.Focus), but the problem I'm having is that I can't seem to get keyboard focus to go to the first focusable element on the relevant view, whatever it may be.

I thought I'd found what I needed here but when using this code to walk the visual tree it seems that when passing the ContentPresenter (OverlayContent) into VisualTreeHelper.GetChildrenCount() that it returns no children, so it can't get down the elements within the dynamic view.

I've also tried setting the OverlayContainer as the active focus scope and then calling:

OverlayContainer.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next))

but that failed to work also.

All the views I am trying to display have at least 1 focusable, visible, enabled element in them.

Any ideas?

Upvotes: 2

Views: 1877

Answers (1)

Ross McCulloch
Ross McCulloch

Reputation: 107

It appears Rachel was spot one - it turned out it was that the view hadn't rendered yet, so the child visual elements weren't available yet. Calling my focuser like so:

// Focus on first child element only once rendered
this.OverlayContainer.Dispatcher.BeginInvoke(DispatcherPriority.Render,
                                             new Action<FrameworkElement>(Focuser.FocusOnFirstFocusableChild),
                                             this.OverlayContainer);

allowed me to access all the child visual elements and set focus accordingly.

Upvotes: 1

Related Questions