Sabyasachi Mishra
Sabyasachi Mishra

Reputation: 1749

Bind Multibinding Textbox in WPF MVVM

I have 3 TextBoxes bind with my class(Transaction) properties like this

<TextBox Text="{Binding Path=Transaction.Bills100,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="bills100" Grid.Column="2" Grid.Row="1" Margin="7"></TextBox>
<TextBox Text="{Binding Path=Transaction.Bill50,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="bills50" Grid.Column="2" Grid.Row="2" Margin="7"></TextBox>
<TextBox Text="{Binding Path=Transaction.Bill20,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Name="bills20" Grid.Column="2" Grid.Row="3" Margin="7"></TextBox>

Also I have another TextBox where I have done multibinding and done addition of the first three Textboxes like

 <TextBox Grid.Column="2" IsReadOnly="True" Grid.Row="7" Grid.ColumnSpan="2" Margin="7" Name="TotalBills">
   <TextBox.Text>
        <MultiBinding Converter="{ikriv:MathConverter}" ConverterParameter="x+y+z" Mode="TwoWay">
            <Binding Path="Text" ElementName="bills100" />
            <Binding Path="Text" ElementName="bills50" />
            <Binding Path="Text" ElementName="bills20" />
         </MultiBinding>
     </TextBox.Text>
 </TextBox>

I want to bind this multibinding textbox with my class(Transaction) with property as Transaction.Total like my first three textboxes but it shows error

Property text is set more than once

Upvotes: 1

Views: 1214

Answers (1)

Sabyasachi Mishra
Sabyasachi Mishra

Reputation: 1749

Actually we cannot get the value of a two-way binding from one property and then set the value of another property. Finally I came with a solution like this In my Class Transaction

private double _totalBills; 
public double TotalBills
{
    get { return _totalBills; }
    set { _totalBills= value; Notify("TotalBills"); }
}

In XAML(Instead of Multibinding)

<TextBox Text="{Binding Path=Transaction.TotalBills,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Grid.Column="2" IsReadOnly="True" Grid.Row="7" Grid.ColumnSpan="2" Margin="7" Name="TotalBills"/>

My ViewModel

 public class MainViewModel: INotifyPropertyChanged
{
    private Transaction _transactionDetails;
    public MainViewModel()
    {
        Transaction= new Transaction();
       _transactionDetails.PropertyChanged += _transactionDetails_PropertyChanged;
    }
    private void _transactionDetails_PropertyChanged(object sender,PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "TotalBills":
                _calculate(); //My method for calculation
                break;
        }
    }
}

Upvotes: 2

Related Questions