如何在WPF中动态地将数据库值绑定到TextBox

时间:2016-11-11 09:42:36

标签: c# wpf

我是WPF的新手 当我单击按钮时,将显示动态TextBox 每当文本框被聚焦时,它将显示网格视图 该gridview包含一个值表 当我输入一行时,它将绑定到文本框,但是当输入值后显示gridview时,它不会显示新值。

 int count = 0;       
    private void button_Click(object sender, RoutedEventArgs e)
    {   
        TextBox t = new TextBox();
        t.MinHeight = 15;
        t.Width = 100;
        t.Height = 30;
        t.Name = "txtPKSourceCode";
        t.Text = "{Binding  PurchaseOrder.PickupSrcCodeName, Mode=TwoWay}";
        ColumnDefinition colDef1;
        colDef1 = new ColumnDefinition();
       mymy.ColumnDefinitions.Add(colDef1);

        RowDefinition rowDef1;
        rowDef1 = new RowDefinition();
        mymy.RowDefinitions.Add(rowDef1);
        ++count;

        mymy.Children.Add(t);
          Grid.SetColumn(t, 0);
        Grid.SetRow(t, count);
  t.GotFocus += t_GotFocus;

     }

    private void t_GotFocus(object sender, RoutedEventArgs e)
    {
        button.Visibility = Visibility.Hidden;
        SourceCodeDialog sourcedlg = new SourceCodeDialog("txtPKSourceCode");
        sourcedlg.Owner = Window.GetWindow(this);
        sourcedlg.POWindow = this;
        var srcresult = sourcedlg.ShowDialog();
    }

1 个答案:

答案 0 :(得分:0)

您可以创建Binding类的对象,您可以在其中指定绑定路径和模式。然后,您可以使用SetBinding()方法设置与文本框的绑定。

TextBox t = new TextBox();

绑定txtBinding = new Binding(“PurchaseOrder.PickupSrcCodeName”); txtBinding.Mode = BindingMode.TwoWay;

.S.SetBinding(TextBox.TextProperty,txtBinding);