0

我有一个堆栈面板,其中在后面的代码中创建了多个用户控件。当我生成这些用户控件时,应用程序中的表格正在填充数据,然后我单击一个按钮,在后面的代码中我想获取填充在表格中的数据,但是当我逐步执行代码时,它看起来不像表格正在刷新/更新/填充新数据。我是否需要将某些东西设置为双向绑定或更新某些东西?这是我的代码。

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);
        }
    }
4

0 回答 0