我开发了一个发票应用程序,并在我的 WPF 应用程序中添加了一个数据网格控件。现在我需要以编程方式将文本框添加到数据网格单元格中。
你能给我一些关于如何在 CellEditingTemplate 中找到文本框的想法吗?请参考此屏幕截图 - 在此先感谢
用户控制测试.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;
}
}