如何将焦点设置到tabcontrol中放置的datagrid

时间:2010-06-01 06:14:43

标签: wpf wpf-controls tabcontrol wpfdatagrid wpftoolkit

我有一个tabcontrol,我将datagrid放在tabcontrol中......但是我无法将该Datagrid聚焦在load事件中......焦点意味着我无法通过键盘导航到它...我得到了这个问题只在选项卡控件内部,否则它工作正常......

我的xaml代码如下......

<Window x:Class="WpfApplication12.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="MainWindow" Height="350" Width="525" WindowState="Maximized" >
 <Border DockPanel.Dock="Top">
  <Grid>
  <TabControl x:Name="tcOpenForms" SelectedIndex="0" TabStripPlacement="Bottom" >
    <DataGrid HorizontalAlignment="Left" AutoGenerateColumns="False" Width="359" Name="grd_boview" Canvas.Left="12" Canvas.Top="12" Height="640" IsReadOnly="True" SelectionMode="Single" HorizontalScrollBarVisibility="Disabled">
     <DataGrid.Columns>
      <DataGridTextColumn Header="BO.ID" Binding="{Binding acct_code}" />
      <DataGridTextColumn Binding="{Binding acct_name}" Header="BO.Name" />
     </DataGrid.Columns>
    </DataGrid>
   </TabControl>
   </Grid>
 </Border>
</Window>

我的C#代码如下

  namespace WpfApplication12
{
 /// <summary>
 /// Interaction logic for MainWindow.xaml
 /// </summary>
 public partial class MainWindow : Window
 {
  public MainWindow()
  {
   InitializeComponent();
   Loaded += new RoutedEventHandler(MainWindow_Loaded);
  }

  void MainWindow_Loaded(object sender, RoutedEventArgs e)
  {

   SqlConnection sqlcon = new SqlConnection("server=ibmserver;uid=sa;pwd=zillion;database=ecdslxyz20102011");
   SqlCommand sqlcmd = new SqlCommand("select * from account", sqlcon);
   SqlDataAdapter sqldac = new SqlDataAdapter(sqlcmd);
   DataSet ds = new DataSet();
   sqldac.Fill(ds, "Test");
   grd_boview.ItemsSource = ds.Tables["Test"].DefaultView;
   grd_boview.Focus();
   grd_boview.SelectedIndex = 0;
  }
 }
}

(1)请注意,焦点意味着我无法通过键盘导航数据网格

(2)它应该在加载事件中......因为它在其他事件中工作正常......

(3)Datagrid应该放在一个标签控件中...因为它在标签控件之外工作正常.....

我希望你能帮助我...

先谢谢

0 个答案:

没有答案