我有一个堆栈面板,在后面的代码中创建了多个用户控件。当我创建这些用户控件时,在应用程序中有表填充数据,然后我单击一个按钮,在后面的代码中我想要获取表中填充的数据,但是当我单步执行代码时,它看起来不像表格正在刷新/更新/填充新数据。我需要设置一些东西来双向绑定或更新某些东西吗? 这是我的代码。
private void TestGridValues_OnClick(object sender, RoutedEventArgs e)
{
StackPanel stackPanel = this.TestGrids;
foreach (UserControl child in stackPanel.Children)
{
Grid content = child.Content as Grid;
ListView view = content.Children[0] as ListView;
ItemCollection itemCollection = view.Items;
DataRowView dataRowView = itemCollection.CurrentItem as DataRowView;
DataTable dataTable = dataRowView.DataView.Table;
}
}
private void MakeGridviewColumns(DataTable table)
{
this.ModelerListView.ItemsSource = table.DefaultView;
GridView gv = new GridView();
this.ModelerListView.View = gv;
// add the date column to gridview
GridViewColumn dateColumn = new GridViewColumn();
dateColumn.Header = "Date";
gv.Columns.Add(dateColumn);
dateColumn.DisplayMemberBinding = new Binding("Date");
foreach (DataColumn dataColumn in table.Columns)
{
if (dataColumn.ColumnName == "Date")
continue;
GridViewColumn gvc = new GridViewColumn();
gvc.Header = dataColumn.ColumnName;
DataTemplate dt = new DataTemplate();
FrameworkElementFactory txtBoxElement = new FrameworkElementFactory(typeof(TextBox));
Binding bind = new Binding();
bind.Path = new PropertyPath("Text");
bind.Mode = BindingMode.TwoWay;
txtBoxElement.SetBinding(TextBox.TextProperty, bind);
txtBoxElement.SetValue(TextBox.TextProperty, "test");
dt.VisualTree = txtBoxElement;
gvc.CellTemplate = dt;
gv.Columns.Add(gvc);
}
}