Using this converter:
public class SumConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType,
object parameter, System.Globalization.CultureInfo culture)
{
double _Sum = 0;
if (values == null)
return _Sum;
foreach (var item in values)
{
double _Value;
if (double.TryParse(item.ToString(), out _Value))
_Sum += _Value;
}
return _Sum;
}
public object[] ConvertBack(object value, Type[] targetTypes,
object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Do this:
<Window.Resources>
<local:SumConverter x:Key="MySumConverter" />
</Window.Resources>
<StackPanel>
<TextBox Name="TextBox1" Text="1" />
<TextBox Name="TextBox2" Text="2" />
<TextBox Name="TextBox3" Text="3" />
<TextBox Name="TextBox4" Text="4" />
<TextBox Name="TextBox5" Text="5" />
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource MySumConverter}"
StringFormat="{}{0:C}"
FallbackValue="Error" TargetNullValue="Null">
<Binding Path="Text" ElementName="TextBox1" />
<Binding Path="Text" ElementName="TextBox2" />
<Binding Path="Text" ElementName="TextBox3" />
<Binding Path="Text" ElementName="TextBox4" />
<Binding Path="Text" ElementName="TextBox5" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
Looks like:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…