繁体   English   中英

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

[英]How to Bind Database Value to TextBox Dynamically in WPF

我是WPF的新手。
当我单击按钮时,将显示一个动态的TextBox。
只要文本框处于焦点位置,它将显示一个gridview。
该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();
    }

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

TextBox t =新的TextBox();

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

t.SetBinding(TextBox.TextProperty,txtBinding);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM