2009-07-31 95 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> 

當然不是很優雅,但它可以快速完成工作。也不需要代碼隱藏。