C# WPF 多重绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1168419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:52:22  来源:igfitidea点击:

WPF MultiBinding

c#wpfdata-bindingmultibinding

提问by David

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.

这在一定程度上可以正常工作。我还希望将送货地址绑定到我的数据库实体,就像账单地址一样。我的问题是,虽然送货地址文本框填充了帐单地址中键入的内容,但在发生这种情况时不会触发 ConvertBack 方法。仅当直接在送货地址文本框中输入内容时才会触发。

What am I missing?

我错过了什么?

采纳答案by Rob Fonseca-Ensor

Maybe this would be easier to implement in your ViewModel?

也许这会更容易在您的 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; }
}