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 :-} )