Note that there are some explanatory texts on larger screens.

plurals
  1. POSet focus to a button in a new TabItem
    text
    copied!<p>I use a TabControl to display a list of items. The ItemView is a separate control I wrote.</p> <pre><code> &lt;TabControl SelectedItem="{Binding CurrentItem}" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding ItemList}"&gt; &lt;TabControl.ContentTemplate&gt; &lt;DataTemplate&gt; &lt;ctrls:ItemView/&gt; &lt;/DataTemplate&gt; &lt;/TabControl.ContentTemplate&gt; &lt;TabControl.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding ShortItemDescription}"/&gt; &lt;/DataTemplate&gt; &lt;/TabControl.ItemTemplate&gt; &lt;/TabControl&gt; </code></pre> <p>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.</p> <pre><code> &lt;Button Command="{Binding AddItemCommand}" Content="Add item"/&gt; </code></pre> <p>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.</p> <pre><code>&lt;UserControl x:Class="ItemView" ... Initialized="ViewInitialized"&gt; &lt;Grid&gt; ... &lt;!-- Set focus to this button --&gt; &lt;Button Content="Search" Command="{Binding SearchCommand}" Name="SearchButton" Grid.Column="0" Grid.Row="0"/&gt; &lt;/Grid&gt; &lt;/UserControl&gt; </code></pre> <p>Any ideas?</p> <p><strong>Update</strong></p> <p>The content of the Initialize method in the ItemView is currently this one - which is working fine... <em>for the first time and then never again</em></p> <pre><code> public delegate void ChangeFocusDelegate(); private void FocusSearchButton() { SearchButton.Focus(); } private void ViewInitialized(object sender, EventArgs e) { ChangeFocusDelegate focusDelegate = FocusSearchButton; Dispatcher.BeginInvoke(DispatcherPriority.ContextIdle, focusDelegate); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload