0

I use a TabControl to display a list of items. The ItemView is a separate control I wrote.

       <TabControl SelectedItem="{Binding CurrentItem}"
                    IsSynchronizedWithCurrentItem="True"
                    ItemsSource="{Binding ItemList}">
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <ctrls:ItemView/>
                </DataTemplate>
            </TabControl.ContentTemplate>
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ShortItemDescription}"/>
                </DataTemplate>
            </TabControl.ItemTemplate>
        </TabControl>

If the user presses a Button a new ViewModel is added to the list of ViewModels and the TabControl displays it as a new tab. After adding the new tab is selected.

       <Button Command="{Binding AddItemCommand}"
               Content="Add item"/>

Inside of the new View is a button that needs to be focused each time a new tab is added. I have tried to use the FocusManager and the Initialized event in the ItemView but these are only called for the first time I add a new tab.

<UserControl x:Class="ItemView"
         ...
         Initialized="ViewInitialized">
<Grid>
    ...

    <!-- Set focus to this button -->
    <Button Content="Search"
            Command="{Binding SearchCommand}"
            Name="SearchButton"
            Grid.Column="0"
            Grid.Row="0"/>
</Grid>
</UserControl>

Any ideas?

Update

The content of the Initialize method in the ItemView is currently this one - which is working fine... for the first time and then never again

    public delegate void ChangeFocusDelegate();

    private void FocusSearchButton()
    {
        SearchButton.Focus();
    }

    private void ViewInitialized(object sender, EventArgs e)
    {
        ChangeFocusDelegate focusDelegate = FocusSearchButton;
        Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, focusDelegate);
    }
Jan
  • 295
  • 2
  • 4
  • 11
  • I think you meant View when your wrote ViewModel. The simpliest solution is to handle "tab_page _added" event in code-behind where set focus on the button – Arseny Dec 29 '10 at 10:59
  • You were right - I changed it. Is there a tab_page_added event? I didn't found anything like that. This would be a solution if something like this exists... – Jan Dec 29 '10 at 15:03

3 Answers3

1

This should be work like this (over your defined Initialized event of the UserControl):

private void ViewInitialized(object sender, System.EventArgs e)
{
    this.SearchButton.Focus();
}
  • As I described above... the Initialized method is only once called! And even then you're solution almost never works :( I will extend my post. – Jan Dec 29 '10 at 14:57
0

You need to subscribe to the Loaded event on your view and in the event handler method call Focus() on your button.

Pavlo Glazkov
  • 20,498
  • 3
  • 58
  • 71
  • Try subscribing to the IsVisibleChanged event and in the event handler, if IsVisible is true, call Focus() on your button. – Pavlo Glazkov Dec 29 '10 at 20:33
0

It seemed to bit a bit strange...
IsVisibleChanged ... no reaction
Initialized ... no reaction
Loaded ... no reaction

And then I got the solution. What if the runtime uses the same View for each tab and changes only the data inside to another ViewModel. So the solution is:

    private void SameViewButDataContextChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
    {
        ChangeFocusDelegate focusDelegate = FocusFileSearchButton;
        Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, focusDelegate);
    }

And each time I add a tab the focus is switched to the Search button.

Jan
  • 295
  • 2
  • 4
  • 11