如何动态地将文本框添加到数据网格单元格以及如何检索输入的数据?使用自定义用户控件

时间:2018-04-24 16:47:18

标签: c# wpf wpfdatagrid datatemplate datagridcell

我开发了一个发票应用程序,并且已经将数据网格控件添加到我的WPF应用程序中。现在我需要以编程方式将文本框添加到数据网格单元格中。

您能否告诉我如何在CellEditingTemplate中找到文本框?请参阅此屏幕截图 - 提前感谢

enter image description here

UserControltest.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="300" d:DesignWidth="300">
    <Grid x:Name="ItemHolder" Height="30">            
    </Grid>        
</UserControl>

**Mainwindow(FrmBill.xmal):**

    <Window x:Class="InvoiceApp.FrmBill"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Testgrid" Height="300" Width="400" Loaded="Window_Loaded">
        <Grid>
            <VirtualizingStackPanel x:Name="MydataGrid" VerticalAlignment="Stretch" Height="350"/>
            <!--Now here i am setting the height to 0,the reason will be explained afterwards-->

        </Grid>
    </Window>

FrmBill.cs(C#代码):

 string str = ConfigurationManager.AppSettings["ConnectInvoice"].ToString();
    SqlConnection con;
    public FrmBill()
    {
        InitializeComponent();
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    con = new SqlConnection(str);
    con.Open();
   SqlCommand cmd = new SqlCommand("Select * from tbl_Tax", con);     
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
UserControltest row = new UserControltest();
int trd = row.ItemHolder.ColumnDefinitions.Count;
if(row.ItemHolder.ColumnDefinitions.Count==0)  
 {
     row.ItemHolder.ColumnDefinitions.Add(new ColumnDefinition());//this will ad required number of columns which will represent the cells
 }    
TextBox txtbx = new TextBox();
txtbx.Height = 20;  
row.ItemHolder.Children.Add(txtbx);
Grid.SetColumn(txtbx,3); /// here 1 is the column count, change it as you want :)
MydataGrid.Children.Add(row);
MydataGrid.Height = MydataGrid.Height + 30;   

}

}

1 个答案:

答案 0 :(得分:0)

正如OP提到的那样,他希望使用自定义User-control,这里是样本User-Control的XAML,可能代表Data-Row

<UserControl x:Class="MyDatarow"
 ......>
  <Grid x:Name="ItemHolder" Height="30" x:FieldModifier ="Public">
   <Grid.ColumnDefinitions>
      <ColumnDefinition Width="20*" />  <!--Create as many column as you want during design time,or you can simply create it dynamically -->
      <ColumnDefinition Width="20*" /><!--Now here i am setting the Width to 20*,instead of this you can set a MinWidth and MaxWidth if you want , i recommend it too-->
      <ColumnDefinition Width="20*" />

  <!--When i write the c# code,i will consider that no column were added here during design time-->
   </Grid.ColumnDefinitions>

现在,让我们将它呈现为DataGrid。让我们假设数据来自数据库,然后我们可以使用IDataReaderVirtualizingStackPanel来实现{{1}看:

datagrid

<强> C#

   <VirtualizingStackPanel x:Name="MydataGrid" VerticalAlignment="Stretch" Height="0"/> <!--Now here i am setting the height to 0,the reason will be explained afterwards-->

希望这能解决您的问题:)