如何将焦点设置到放置在选项卡控件内的数据网格

发布于 2024-09-03 20:22:31 字数 2067 浏览 4 评论 0原文

我有一个选项卡控件,并将数据网格放置在选项卡控件内...但我无法在加载事件中聚焦该数据网格...焦点意味着我无法通过键盘导航到它...我只在内部遇到此问题的选项卡控件,否则它工作正常...

我的 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 应该放置在选项卡控件内...因为它在选项卡控件之外工作正常......

我希望你能帮助我。 ..

提前致谢

I have a tabcontrol and I place datagrid inside a tabcontrol... But I could not focus that Datagrid at the load event ... Focus means I couldn't navigate through keyboard into it... I'm getting this problem only inside of the tab control otherwise it works fine...

My xaml code as follows...

<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>

My C# code as follows

  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) Please note that focus means I couldn't navigate a datagrid through keyboard

(2) It should be in load event......Because it works fine in other events...

(3) Datagrid should be placed inside a tab control... Because it works fine outside a tab control.....

I hope u will help me...

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文