2009-07-31 98 views
2

我在WPF 一个TabControl当我切换到特定的TabItem,我想将焦点设置上的特定的textBox的TabControl和设置焦点上的文本框在WPF

我添加textBox_n.Focus的代码();在selectionChanged的事件处理程序中,但它不起作用。

我在tabItem的GotFocus的事件处理程序中添加了代码,但足够有趣的调用textBox_n.Focus(),它再次调用tabItem的GotFocus。

所以在哪里以及哪里放置它最好的地方。

回答

0

如果您使用网格排列文本框,您可以将要聚焦的网格放置为网格的第一个子节点,并将其行和列指定为第二个或第三个,这里是一个示例。

<TabControl> 
     <TabItem Header="Tab 1"> 

     </TabItem> 
     <TabItem Header="Tab 2"> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 
       <TextBox Grid.Row="1" Margin="5">textBox2</TextBox> <!-- This textbox is the first child of the grid, so it gets focused --> 
       <TextBox Grid.Row="0" Margin="5">textBox1</TextBox> <!-- This textbox is catually on top of textBox2 --> 
      </Grid> 
     </TabItem> 
    </TabControl> 

当然不是很优雅,但它可以快速完成工作。也不需要代码隐藏。