N
N
Nikita2016-06-18 22:52:11
WPF
Nikita, 2016-06-18 22:52:11

How to automatically recalculate values?

Good evening everyone.
I have a set of TextBoxes and I have a Pressure object. Each property of this object is a type of pressure unit (pascal, bar, etc.) I have attached all properties to my TextBox.
Tell me how to implement automatic recalculation of values ​​in the ViewModel? (I understand that I must implement INotifyPropertyChanged)
At the same time, one must somehow take into account that the recalculation is carried out according to different formulas (one from pascal to bar, another from pascal to mmHg).
Maybe, of course, I'm wrong that I created one object, but do I need to make my own object for each type?
I will be glad to receive advice.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WarFollowsMe, 2016-06-20
@Smiz001

ViewModel with pressure value

public class PressureViewModel:INotifyPropertyChanged
{
    private double _pressure;

    public double Pressure
    {
        get{return _pressure;}
        set
        {
            _pressure = value;
            SendPropertyChanged("Pressure");
        }
    }

    protected void SendPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

converter code:
public class PaskalToBarConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        // метод преобразования паскаль в бар
        return PaskalToBar((double)value);
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        //метод преобразования бар в паскаль
        return BarToPaskal((double)value);
    }
}

add a link to the namespace of the converter in the xaml:
add a converter in resources:
<Window.Resources>
    <converters:PaskalToBarConverter x:Key="PaskalToBar"/>
</Window.Resources>

well, in the TextBox we bind the pressure + converter:
<TextBox x:Name="PaskalPressure" Text={Binding Pressure}/>
<TextBox x:Name="BarPressure" Text={Binding Pressure, Converter={StaticResource PaskalToBar}}/>

Do the same for mm.r.st

#
#algooptimize #bottize, 2016-06-18
@user004

If I understand correctly and there is one physical quantity that is displayed in different units - then ivalueconverter

N
Nikolai Kiselev, 2016-06-19
@russlanddiv

If I correctly understood the question of the TS, that is, 3 TextBoxes: Pascal, Bar, MMRtuti (numerical values)
Then on the KeyUp of each, hang an event that recalculates the values ​​of the rest.
For example:
* Press '1' in Pascal
* "KeyUp" event
* Bar.Text = functionPascalВBar(Pascal.Text)
* MMPtuty.Text = functionPascalVMMRtuty(Pascal.Text)
Accordingly, add a converter or parser to double in my example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question