1

I have 10 textboxes and I want there addition to be shown in a single textblock on lost_focus UpdateSourceTrigger property.

2
  • Do you want to concatenate string or Sum of all the numbers entered Commented Mar 5, 2015 at 17:03
  • Sum of all the integers Commented Mar 5, 2015 at 17:08

1 Answer 1

1

If you need to update the sum on TextBox lost focus event you can use classic events. This is the XAML (I used just four TextBoxes, but it is easy to extend):

<StackPanel>
    <TextBox Name="txt01" Margin="3" HorizontalAlignment="Stretch" LostFocus="txt_LostFocus" TextChanged="txt_TextChanged" />
    <TextBox Name="txt02" Margin="3" HorizontalAlignment="Stretch" LostFocus="txt_LostFocus" TextChanged="txt_TextChanged" />
    <TextBox Name="txt03" Margin="3" HorizontalAlignment="Stretch" LostFocus="txt_LostFocus" TextChanged="txt_TextChanged" />
    <TextBox Name="txt04" Margin="3" HorizontalAlignment="Stretch" LostFocus="txt_LostFocus" TextChanged="txt_TextChanged" />
    <TextBlock Name="sum" Margin="3,10,3,3" />
</StackPanel>

In the code you have the event handlers:

private void txt_LostFocus(object sender, RoutedEventArgs e)
{
    int value1;
    int value2;
    TextBox textBox = (TextBox)sender;

    if (textBox.Tag is bool && (bool)textBox.Tag)
    {

        if (Int32.TryParse(textBox.Text, out value1))
        {
            if (String.IsNullOrEmpty(sum.Text))
            {
                sum.Text = textBox.Text;
            }
            else
            {
                Int32.TryParse(sum.Text, out value2);
                sum.Text = Convert.ToString(value1 + value2);
            }
        }

        textBox.Tag = false;
    }
}

private void txt_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textBox = (TextBox)sender;
    textBox.Tag = true;
}

On the other side, if you can give up the "LostFocus" requirement, you can use MultiBinding (in this case it works just in "PropertyChanged mode", since TextBoxes are now the sources):

<StackPanel>
    <TextBox Name="txt01" Margin="3" HorizontalAlignment="Stretch" />
    <TextBox Name="txt02" Margin="3" HorizontalAlignment="Stretch" />
    <TextBox Name="txt03" Margin="3" HorizontalAlignment="Stretch" />
    <TextBox Name="txt04" Margin="3" HorizontalAlignment="Stretch" />
    <TextBlock Name="sum" Margin="3,10,3,3">
        <TextBlock.Text>
            <MultiBinding Converter="{StaticResource AddValueConverter}" Mode="OneWay">
                <MultiBinding.Bindings>
                    <Binding ElementName="txt01" Path="Text" />
                    <Binding ElementName="txt02" Path="Text" />
                    <Binding ElementName="txt03" Path="Text" />
                    <Binding ElementName="txt04" Path="Text" />
                </MultiBinding.Bindings>
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
</StackPanel>

You just need to write a simple converter:

public class AddValueConverter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        int sum = 0;
        int result;

        foreach(object value in values)
        {
            if (Int32.TryParse(System.Convert.ToString(value), out result))
            {
                sum += result;
            }
        }

        return System.Convert.ToString(sum);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.