I have been trying to find a solution to this one but could not find a relevant answer online.
I have a bunch of text boxes that I need to populate with data, for example in my case address. I have a combo box that is bound to an enum and has a list of values to select from
Home, Office, MainOffice, Laboratory. etc. When I make a selection on combobox, I need to populate its address in the text boxes below. I can get Home address from object X, Office Address from Object Y, MainOffice from Z. How do I do this conditional databinding using combobox selection. Please advise.
These are the options that I could select
public enum MailToOptions
{
Home,
Office,
CorporateOffice,
Laboratory,
}
<!-- Row 1 -->
<Label Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="5" Content="Mail To:" />
<ComboBox Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="5" Name="MailToComboBox"
ItemsSource="{Binding Source={StaticResource odp}}"
SelectionChanged="HandleMailToSelectionChangedEvent" >
</ComboBox>
<!-- Agency ID -->
<!-- Name -->
<Label Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="5" Content="Name" />
<TextBox Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="5">
<TextBox.Text>
<Binding Path="Order.Agency.PartyFullName" Mode="TwoWay" />
</TextBox.Text>
</TextBox>
<!-- Row 2 -->
<!-- Address Line 1 -->
<Label Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="5" Content="Address 1" />
<TextBox Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="5">
<TextBox.Text>
<Binding Path="Order.Agency.AddressLine1" Mode="TwoWay" />
</TextBox.Text>
</TextBox>
The best way would be to make the items in your
ComboBoxobjects, and bind your text fields to theComboBox.SelectedItemFor example,
A cleaner way would be to set the
DataContextof whatever panel holds the TextBoxesYou can either bind the ComboBox to something like an
ObservableCollection<Address>, or set theItemsSourcemanually in the code behind.I’d recommending binding, however to set it manually in the code behind it would look something like this: