如何在UserControl中动态添加文本框以及如何将焦点放在Wpf ..?

时间:2018-04-28 23:53:11

标签: c# wpf datagrid user-controls

我开发了一个发票应用程序,我在我的WPF Application.i'm中添加了一个带有2个以上文本框的Usercontrol,用于textbox1的itemcode中的搜索选项,显示textbox2用于从数据库数据中获取值的项目成功显示,现在我需要textbox如何在Usercontrol中动态添加并焦点到下一行,如果填充1行然后焦点到下一行。请找到下面的代码我在应用程序中犯了什么错误请帮助我的朋友。谢谢你提前< / p>

**Usercontrol.xaml**

<UserControl x:Class="InvoiceApp.UserControltest"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="250" d:DesignWidth="560" VerticalAlignment="Top" Loaded="UserControl_Loaded">

    <Grid x:Name="ItemHolder"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Height="250" Width="560">

        <TextBox x:Name="txtbx1" Height="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="111" KeyDown="txtbx1_KeyDown" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtbx2" Height="20" HorizontalAlignment="Left" Margin="110,0,0,0"  VerticalAlignment="Top" Width="92" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtbx3" Height="20" HorizontalAlignment="Left" Margin="201,0,0,0" VerticalAlignment="Top" Width="85" BorderBrush="#FFC5C7CB"/>
        <TextBox x:Name="txtbx4" Height="20" HorizontalAlignment="Left" Margin="285,0,0,0" VerticalAlignment="Top" Width="85" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtbx5" Height="20" HorizontalAlignment="Left" Margin="369,0,0,0" VerticalAlignment="Top" Width="85" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtbx6" Height="20" HorizontalAlignment="Left" Margin="453,0,0,0" VerticalAlignment="Top" Width="78" BorderBrush="#FFC5C7CB" />
        <TextBox x:Name="txtId" Height="20" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="22" Margin="536,1,0,0" BorderBrush="#FFC5C7CB" />

    </Grid>

</UserControl>

**Usercontroltest.cs**



 private void txtbx1_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox txtbx = (TextBox)sender;
            if (e.Key == Key.Enter)
            {
                SqlCommand cmd = new SqlCommand("Select * from tbl_Tax WHERE Tax=@tax", InvoiceApp.FrmBill.con);
                cmd.Parameters.AddWithValue("@tax", txtbx.Text);
               // SqlDataReader dr = cmd.ExecuteReader();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables[0].Rows.Count > 0)
                {

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        txtbx2.Text = ds.Tables[0].Rows[0]["Id"].ToString();
                       // txtbx3.Text = dr[3].ToString();
                        txtbx3.Text = ds.Tables[0].Rows[0]["Tax"].ToString();
                        txtbx4.Text = ds.Tables[0].Rows[0]["TaxAmount"].ToString();
                        txtbx5.Text = ds.Tables[0].Rows[0]["Description"].ToString();
                      //  txtbx6.Text = ds.Tables[0].Rows[0]["Description"].ToString();
                       // txtId.Text = ds.Tables[0].Rows[0]["Id"].ToString();
                        Items.Rows.Add(txtbx2.Text, txtbx3.Text, txtbx4.Text, txtbx5.Text);

                       // dataGrid1.ItemsSource = Items.DefaultView;
                        //dataGrid1.DataContext = Items;
                       // dattt();
                    }

                    //if (grdQustion.Rows.Count >= 1)
                    //{
                    //    int i = grdQustion.CurrentRow.Index + 1;
                    //    if (i >= -1 && i < grdQustion.Rows.Count)
                    //        grdQustion.CurrentCell = grdQustion.Rows[i].Cell[0];
                    //}
                }
                ds.Dispose();
            }

1 个答案:

答案 0 :(得分:0)

您可以将元素作为子元素添加到现有元素中。首先创建它,然后随时更改其属性并添加到元素中,这里是一个Grid。

TextBox newchild = new TextBox();
ItemHolder.Children.Add(newchild);