WPF-xaml Calculating Total Text Field Values

I have a wpf xaml form that has 5 text fields showing the price of an order. Below 5 text fields, I have another text field: [subTotal], which displays the subtotal of the order price. "SubTotal" The text box should automatically display the subtotals of the orders.

Is there any way to encode XAMl when I can automatically calculate and distribute the totals in the SubTotal text box when the user enters the value in the order text boxes.

+3
source share
2 answers

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

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

It looks like:

screenshot

+10
source

, ( 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 .

+2

Source: https://habr.com/ru/post/1711736/


All Articles