Kevin Hernandez
Kevin Hernandez

Reputation: 525

Programmatically Set Data Binding in WPF for Dynamic TextBoxes from a Dynamic ComboBox

This part works.

In my C#.NET WPF XAML, I have a static ComboBox and a static TextBox. The TextBox displays another column from the same DataTable (in the ComboBox's ItemSource). The column "rr_code" is the column for company name and the column "rr_addr" is the column for the address.

<ComboBox x:Name="CompanyComboBox1" IsEditable="True" IsTextSearchEnabled="True" IsSynchronizedWithCurrentItem="False"/>
<TextBox x:Name="StreetTextBox1" DataContext="{Binding SelectedItem, ElementName=CompanyComboBox1}" Text="{Binding rr_addr}" IsManipulationEnabled="True"\>

The ComboBox reads programmatically from a column in a DataTable:

 CompanyComboBox1.ItemsSource = Rails.DefaultView; // Rails is a DataTable
 CompanyComboBox1.DisplayMemberPath = "rr_code"; // column name for company name

This part doesn't work

The question is, I now have a "Add Company" button that creates a new form in a StackPanel, dynamically and with this exact functionality. The ComboBox works exactly as expected. Here's what I have so far:

ComboBox companyComboBox = new ComboBox();
companyComboBox.ItemsSource = Rails.DefaultView;
companyComboBox.IsEditable = true;
companyComboBox.IsTextSearchEnabled = true;
companyComboBox.DisplayMemberPath = "rr_code";

The problem is in the TextBox, which is not updating when I change the dynamic companyComboBox, so I'm sure it has to do with the binding.

TextBox streetTextBox = new TextBox();
streetTextBox.DataContext = companyComboBox;
Binding b = new Binding("rr_addr");
b.Mode = BindingMode.Default;
b.Source = companyComboBox.SelectedItem;
streetTextBox.SetBinding(ComboBox.SelectedItemProperty, b);

What is the correct way to set the binding for the TextBox streetTextBox?

Upvotes: 2

Views: 6172

Answers (2)

Kevin Hernandez
Kevin Hernandez

Reputation: 525

The code-behind equivalent of this particular XAML + C# data binding in pure C# is:

ComboBox companyComboBox = new ComboBox();
companyComboBox.ItemsSource = Rails.DefaultView;  // Rails being DataTable
companyComboBox.IsEditable = true;
companyComboBox.IsTextSearchEnabled = true;
companyComboBox.DisplayMemberPath = "rr_code";

Binding b = new Binding("SelectedItem.rr_addr");  // The selected item's 'rr_addr' column ...
b.Source = companyComboBox;                       // ... of the companyComboBox ...

TextBox streetTextBox = new TextBox();    
streetTextBox.SetBinding(TextBox.TextProperty,b); // ... is bound to streetTextBox's Text property.

The error was in the last line. SetBinding needed to have a property of the target, not the source. In addition, the Binding declaration needed "SelectedItem." for some reason.

Upvotes: 3

Luca
Luca

Reputation: 1626

Why are you setting the TextBox DataContext ?

You can simply bind TextBox.Text property to ComboBox SelectedItem in your XAML

<TextBox Text="{Binding ElementName=CompanyComboBox1, Path=SelectedItem.rr_addr}"></TextBox>

Upvotes: 2

Related Questions