Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

textbox - WPF-xaml Calculating Total of text box values

I have a wpf xaml form which has 5 text boxes shows order price. Below the 5 text boxes i have another textbox:[subTotal] which displays the subtotal of order price's."SubTotal" Textbox should display the subtotal of order prices automatically.

Is there any XAMl coding way where i can calculate and dispaly total in the "SubTotal" text box automatically, when user enters a value in the order prices text boxes.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

screenshot


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...