【问题标题】:How to Bind Database Value to TextBox Dynamically in WPF如何在 WPF 中将数据库值动态绑定到 TextBox
【发布时间】:2016-11-11 09:42:35
【问题描述】:

我是 WPF 新手。
当我单击按钮时,将显示一个动态文本框。
每当文本框获得焦点时,它都会显示一个网格视图。
该网格视图包含一个值表。
当我输入一行时,它将绑定到文本框,但是当输入值后显示 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();
    }

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

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

    文本框 t = new TextBox();

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

    t.SetBinding(TextBox.TextProperty, txtBinding);

    【讨论】:

    • 非常感谢您的显示。但是以这种方式旧文本框值绑定在这里我需要在 gridview 值中插入新值
    • 如何将此代码提供给动态文本框
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多