更新时间:2023-12-02 23:14:16
首先,这不是WPF方法.使用适当的绑定来实现您想要的.
First of all this is not a way to do WPF way. Use proper bindings to achieve you want.
以WPF方式执行的步骤:
Steps to do in WPF way:
ObservableCollection< MyGrid>
并将ItemsSource与该集合绑定. INotifyCollectionChanged
以来,DataGrid将自动刷新.现在,对于您的代码,有几个问题.
Now, for your code there are couple of issues.
删除for循环
.初始化列表一次.
无需再次设置ItemsSource
.public class CodeBehindClass
{
private ObservableCollection<MyGrid> gride;
public CodeBehindClass()
{
gride = new ObservableCollection<MyGrid>();
dataGridView1.ItemsSource = gride;
}
private void ButtonHandler(object sender, RoutedEventArgs e)
{
var myg1 = new MyGrid(textBox10.Text, textBox11.Text, textBox12.Text);
gride.Add(myg1);
}
}