I have two text boxes, one for a billing address field and one for a shipping address field. When the user types something into the the billing address text box the shipping address text box gets the same value due to the following binding scenario:
<TextBox Name="txtBillingAddress" Text="{Binding BillingAddress, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
<TextBox Name="txtShippingAddress">
<TextBox.Text>
<MultiBinding Converter="{StaticResource AddressConverter}">
<Binding ElementName="txtBillingAddress" Path="Text" Mode="OneWay" />
<Binding Path="ShippingAddress" UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" />
</MultiBinding>
</TextBox.Text>
</TextBox>
This works fine up to a point. I also want the shipping address to be bound to my database entity as the billing address is. My problem is that while the shipping address text box is populated with what is typed in the billing address, the ConvertBack method is not fired while this is happening. It is only fired if something is typed directly into the shipping address text box.
What am I missing?
MultiBinding Class in WPF. The class MultiBinding allows you to associate a target property of the association with a list of source properties and then apply logic to produce a value with the inputs specified. This example shows how to use the class MultiBinding to display multiple properties of the Customer class in a single line in a ListBox.
Multibinding takes multiple values and combines them into another value. There are two ways to do multibinding, either using StringFormat or by a converter. The StringFormat is simple compared to a converter, so we will start with that first.
This article explains the Multibinding and IMultiValueConverter in WPF. In some cases we might have several fields in your data model that represent a name (first name, middle name and last name) that you want to combine into a single entry in a list. Multibinding takes multiple values and combines them into another value.
So far they have all been based on a single property, but WPF also supports multi triggers, which can monitor two or more property conditions and only trigger once all of them are satisfied.
Maybe this would be easier to implement in your ViewModel?
public string BillingAddress{
set{
billingAddress = value;
firePropertyChanged("BillingAddress");
if(string.isNullOrEmpty(ShippingAddress)
{
ShippingAddress = value; //use the property to ensure PropertyChanged fires
}
}
get{ return billingAddress; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With