WPF-xaml计算文本框值的总和

时间:2009-07-02 12:43:49

标签: wpf textbox

我有一个wpf xaml表单,其中有5个文本框显示订单价格。 在5个文本框下方,我有另一个文本框:[subTotal]显示订单价格的小计。“SubTotal”文本框应自动显示订单价格的小计。

当用户在订单价格文本框中输入值时,是否有任何XAMl编码方式可以在“SubTotal”文本框中自动计算和显示总计。

2 个答案:

答案 0 :(得分:10)

使用此转换器:

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();
    }
}

这样做:

<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>

看起来像:

screenshot

答案 1 :(得分:2)

如果使用绑定(以及一点MVVM模式),这将非常简单。将表单的DataContext设置为以下内容:

using System;
using System.ComponentModel;

namespace WpfApplication1
{
    [Serializable]
    public class TestViewModel : INotifyPropertyChanged
    {
        private decimal value1;
        private decimal value2;
        private decimal value3;
        private decimal value4;
        private decimal value5;

        public TestViewModel()
        {
        }

        public decimal Value1
        {
            get { return value1; }
            set
            {
                value1 = value;
                RaisePropertyChangedEvent("Value1");
                RaisePropertyChangedEvent("SubTotal");
            }
        }
        public decimal Value2
        {
            get { return value2; }
            set
            {
                value2 = value;
                RaisePropertyChangedEvent("Value2");
                RaisePropertyChangedEvent("SubTotal");
            }
        }
        public decimal Value3
        {
            get { return value3; }
            set
            {
                value3 = value;
                RaisePropertyChangedEvent("Value3");
                RaisePropertyChangedEvent("SubTotal");
            }
        }
        public decimal Value4
        {
            get { return value4; }
            set
            {
                value4 = value;
                RaisePropertyChangedEvent("Value4");
                RaisePropertyChangedEvent("SubTotal");
            }
        }
        public decimal Value5
        {
            get { return value5; }
            set
            {
                value5 = value;
                RaisePropertyChangedEvent("Value5");
                RaisePropertyChangedEvent("SubTotal");
            }
        }
        public decimal SubTotal
        {
            get
            {
                return this.value1 + this.value2 + this.value3 + this.value4 + this.value5;
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChangedEvent(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

显然有很多不同的方法可以做到这一点,但如果你将文本框绑定到这里的值,当1个文本框被更改时,它将告诉视图(通过PropertyChanged事件)值和SubTotal被更新。然后屏幕将根据计算的内容更新SubTotal的显示值。

如果您不熟悉绑定或如何设置表单的DataContext,我可以对此进行扩展。