private decimal depositTotal; > > public decimal DepositTotal > { > get > { > return this.depositTotal; > } > > set > { > if (value != this.depositTotal) > { > this.depositTotal = value; > this.OnPropertyChanged("DepositTotal"); > } > } > } > > private decimal balanceOutstanding; > > public decimal BalanceOutstanding > { > get > { > return this.balanceOutstanding; > } > > set > { > if (value != this.balanceOutstanding) > { > this.balanceOutstanding = value; > this.OnPropertyChanged("BalanceOutstanding"); > } > } > }>
<TextBox Name="txtDepositTotal" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DepositTotal}"/>>
<TextBox Name="txtBalanceOutstanding"Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=BalanceOutstanding}" />>
<TextBox Text="{Binding dep_amount, Mode=Default}" > Name="txtDep_Amount" > VerticalAlignment="Center" />>
public class BalanceConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var valuesToConvert = values.Cast<string>().ToArray(); double val1; double val2; if (!Double.TryParse(valuesToConvert[0], out val1)) val1 = 0; if (!Double.TryParse(valuesToConvert[1], out val2)) val2 = 0; return String.Format("Balance: {0}", (val1 - val2).ToString("C")); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:WpfApplication3"> <Window.Resources> <local:BalanceConverter x:Key="balance"/> </Window.Resources> <StackPanel> <TextBox Name="txtDepositTotal" /> <TextBox Name="txtDep_Amount" VerticalAlignment="Center" /> <TextBox Name="txtBalanceOutstanding" > <MultiBinding Converter="{StaticResource balance}"> <Binding ElementName="txtDepositTotal" Path="Text"></Binding> <Binding ElementName="txtDep_Amount" Path="Text"></Binding> </MultiBinding> </TextBox> </StackPanel> </Window>(Converter logic could be tidier :-} )