Answer the question
In order to leave comments, you need to log in
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
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;
}
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);
}
}
<Window.Resources>
<converters:PaskalToBarConverter x:Key="PaskalToBar"/>
</Window.Resources>
<TextBox x:Name="PaskalPressure" Text={Binding Pressure}/>
<TextBox x:Name="BarPressure" Text={Binding Pressure, Converter={StaticResource PaskalToBar}}/>
If I understand correctly and there is one physical quantity that is displayed in different units - then ivalueconverter
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 questionAsk a Question
731 491 924 answers to any question