0

I am using a WPF textbox inside an element host in a Winforms project.

I would like to databind this textbox to a bindingsource as I do with the standard winforms textbox like so:

    Me.tbxCorrectiveAction.DataBindings.Add("Text", bgsTasks, "CorrectiveAction", False)

This is what I have tried:

    Dim host As New System.Windows.Forms.Integration.ElementHost()
    Dim wpfTextBox As New System.Windows.Controls.TextBox()
    wpfTextBox.SpellCheck.IsEnabled = True

    host.Dock = DockStyle.Fill
    host.Child = wpfTextBox
    Me.Panel8.Controls.Add(host)

    Dim binCorrectiveAction As New System.Windows.Data.Binding("CorrectiveAction")
    binCorrectiveAction.Source = bgsTasks
    wpfTextBox.SetBinding(System.Windows.Controls.TextBlock.TextProperty, binCorrectiveAction)

Solutions in either VB or C# are fine.

Reafidy
  • 8,240
  • 5
  • 51
  • 83

1 Answers1

1

Try this:

UPDATE.

There's an error in your code (or just a typo, which causes a logical error).
You're trying to bind TextBlock.TextProperty on a TextBox control.

There should be TextBox.TextProperty:

        var dataTable = new DataTable();

        dataTable.Columns.Add("Id", typeof(Int32));
        dataTable.Columns.Add("Name", typeof(String));

        dataTable.Rows.Add(1, "John");
        dataTable.Rows.Add(2, "Mary");
        dataTable.Rows.Add(3, "Peter");
        dataTable.Rows.Add(4, "Helen");

        var bindingSource = new BindingSource();
        bindingSource.DataSource = dataTable;

        var binding = new System.Windows.Data.Binding("Name");
        binding.Source = bindingSource;
        binding.UpdateSourceTrigger = System.Windows.Data.UpdateSourceTrigger.PropertyChanged;

        var textBox = new System.Windows.Controls.TextBox();
        textBox.SpellCheck.IsEnabled = true;
        textBox.SetBinding(System.Windows.Controls.TextBox.TextProperty, binding);

        elementHost1.Child = textBox;

The reason is that these dependency properties (and controls) are differ, in spite of they have similar names.

If it isn't a typo, then I recommend you to read about WPF dependency properties mechanism here.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • I am just using a standard bindingsource which has its datasource set to a datatable. `bgsTasks.DataSource = TasksDataSet.Tasks` – Reafidy Aug 21 '12 at 23:18
  • @Reafidy: I've looked at your code more accurate and have updated my answer. – Dennis Aug 22 '12 at 05:40
  • thanks, your correct that was a mistake. However it still doesn't appear to fix the problem. The textbox doesnt get bound to the bindingsource. I replaced the wpf box with a normal textbox and used my normal databinding method to check that the binding works with the datasource and it does. – Reafidy Aug 23 '12 at 01:56
  • @Reafidy: please, post your full, non-working code sample here. There's no magic in binding. Also, have you see any binding errors in VS output window? – Dennis Aug 23 '12 at 05:14
  • 1
    thanks for your patience. I discovered that the problem was that I was filling the datatable AFTER setting up the binding. This works for the standard textbox but not when using the WPF textbox. I have to first fill the dataset and then bind the textbox. Your answer and comments really helped. Thanks again. – Reafidy Aug 23 '12 at 11:15