在WPF中动态添加行到gridview
请帮助我编写以下代码,我想将用户输入的行添加到网格视图中。 我可以添加一行,但它是空的!请帮忙。它可以在 Windows 窗体中运行,但不能在 WPF 中运行。
private void button1_Click(object sender, RoutedEventArgs e)
{
GetGridView();
}
private void GetGridView()
{
string[] row0 = {textBox1.Text,"Beatles" };
dataGrid1.Items.Add(row0);
dataGrid1.Columns[0].DisplayIndex = 0;
dataGrid1.Columns[1].DisplayIndex = 1;
}
/////////////////////////// 当然,就是这里
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="964">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="274" HorizontalAlignment="Left" Margin="509,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="239" DataContext="{Binding}" ItemsSource="{Binding}" ItemStringFormat="{Binding}" SelectedIndex="-1" SelectionChanged="dataGrid1_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Header1" />
<DataGridTextColumn Header="Header" />
</DataGrid.Columns>
</DataGrid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,187,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,125,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,66,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="414,231,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
Please help me with the following code,I want to add a row inputted by user to a gridview.
I am able to add a row but its empty!!Please help.it worked in windows forms but its not working with WPF.
private void button1_Click(object sender, RoutedEventArgs e)
{
GetGridView();
}
private void GetGridView()
{
string[] row0 = {textBox1.Text,"Beatles" };
dataGrid1.Items.Add(row0);
dataGrid1.Columns[0].DisplayIndex = 0;
dataGrid1.Columns[1].DisplayIndex = 1;
}
//////////////
sure,here it is
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="964">
<Grid>
<DataGrid AutoGenerateColumns="False" Height="274" HorizontalAlignment="Left" Margin="509,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="239" DataContext="{Binding}" ItemsSource="{Binding}" ItemStringFormat="{Binding}" SelectedIndex="-1" SelectionChanged="dataGrid1_SelectionChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Header1" />
<DataGridTextColumn Header="Header" />
</DataGrid.Columns>
</DataGrid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" TextChanged="textBox1_TextChanged" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,187,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,125,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="184,66,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="414,231,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑:您绑定了 DataGrid 的 ItemsSource,在这种情况下您无法将项目添加到网格本身,将项目添加到绑定的集合(这是我最初建议的)
我不会建议您做类似的事情。在 WPF 中,您应该 绑定您的控件到数据,这样您就可以更改源集合,并且网格将自动更新,这比使用任何接受
类型的输入的方法(例如
DataGrid.Items.Add
)更简洁对象。例如
XAML:
代码:
Edit: You bound the ItemsSource of the DataGrid, you cannot add items to the grid itself while that is the case, add the items to the bound collection (which is what i originally suggested)
I would not suggest you do anything like that. In WPF you should bind your controls to the data, that way you can change the source-collection and the grid will get updated automatically, which is less messy than using any method like
DataGrid.Items.Add
which accepts input of typeobject
.e.g.
Xaml:
Code: