WPF TabBarControl Setting Focus to element when tab changes

Posted by Aran Mulholland on Stack Overflow See other posts from Stack Overflow or by Aran Mulholland
Published on 2010-03-24T23:10:13Z Indexed on 2010/03/24 23:13 UTC
Read the original article Hit count: 552

Filed under:
|
|
|

I have a TabControl that is bound to a view model

          <TabControl
             ItemsSource="{Binding Path=ViewModelCollection}" >
             <TabControl.ItemContainerStyle>
                <Style
                   TargetType="TabItem"
                   BasedOn="{StaticResource {x:Type TabItem}}">
                   <Setter
                      Property="Header"
                      Value="{Binding Title}" />
                   <Setter
                      Property="Content"
                      Value="{Binding}" />
                </Style>
             </TabControl.ItemContainerStyle>
          </TabControl>

Each Tab simply contains a View Model Item. I use a data template to display this.

  <!-- View Model Template -->
  <DataTemplate
     DataType="{x:Type local:ViewModelItem}">
     <DockPanel>
        <TextBox Text="I want this to have the focus"/>
     </DockPanel>
  </DataTemplate>

When the current tab is changed i want the focus to be on the textbox (this is a simple example, in my production code i have a datagrid) in the data template. how do i accomplish this?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about datatemplate