C# 如何向datagrid单元格动态添加文本框,以及如何检索输入的数据?使用自定义用户控件
我已经开发了一个invoice应用程序,并在WPF应用程序中添加了datagrid控件。现在,我需要以编程方式将文本框添加到datagrid单元格中 你能告诉我如何在CellEditingTemplate中找到文本框吗?请参考此截图-提前感谢 UserControltest.xamlC# 如何向datagrid单元格动态添加文本框,以及如何检索输入的数据?使用自定义用户控件,c#,wpf,wpfdatagrid,datatemplate,datagridcell,C#,Wpf,Wpfdatagrid,Datatemplate,Datagridcell,我已经开发了一个invoice应用程序,并在WPF应用程序中添加了datagrid控件。现在,我需要以编程方式将文本框添加到datagrid单元格中 你能告诉我如何在CellEditingTemplate中找到文本框吗?请参考此截图-提前感谢 UserControltest.xaml <UserControl x:Class="InvoiceApp.UserControltest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/p
<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>
}
}正如OP所提到的,他想使用一个自定义的
用户控件
,下面是一个用于示例用户控件
的XAML,它可能表示一个数据行
<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>
C#
希望这能解决你的问题:)你真的想用datagrid吗?我的意思是,如果你愿意,你可以使用自定义用户控件:)是的,正如它所说的@zackraiyan,使用个性化控件会更容易,尽管我并不是说你想要的是不可能的,请告诉我如何使用自定义用户控件control@Anbu,如果您想要自定义用户控件,请编辑您的问题并在此处提及,然后我会发布一个答案:)@zackraiyan谢谢你的回答我编辑了这个问题请分享这个答案谢谢@zack给我一些时间我会实现这个编码并与你分享result@Anbu,OK:)如何将用户控件添加到wpf窗口中,如xmlns:local=“clr namespace:InvoiceApp”我无法导入到usercontrol,我可以添加到任何引用吗?在同一个项目中创建usercontrol,构建它,然后你会在工具箱中看到它,但是在这里,你不需要在设计期间将usercontrol添加到窗口中,因为我们正在动态添加它为延迟响应道歉我已经编写了此代码无论你发布了什么我得到的输出是空的请查找代码我已经编辑了问题我犯了此代码的错误
<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>
<VirtualizingStackPanel x:Name="MydataGrid" VerticalAlignment="Stretch" Height="0"/> <!--Now here i am setting the height to 0,the reason will be explained afterwards-->
SqlConection con=new SqlConnection("connection string here")
SqlCommand cmd = new SqlCommand("Select * from table",con)
SqlDatareader dr = cmd.ExecuteReader();
While (dr.Read())
{
MyDatarow row = new MyDatarow;
If (row.ItemHolder.ColumnDefinition.Count = 0)
{
row.ItemHolder.ColumnDefinition.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,1) /// here 1 is the column count, change it as you want :)
MyDatagrid.Childer.Add(row);
MyDatagrid.height = MyDatagrid.height + 30 ;
}