正如标题所示,我正在寻求更新子类中的计算值。我的WPF页面中有FPBViewModel的Parameters
集合作为DataGrid的ItemSource。 HeatingAirflow
只是MinAirflow
和FanAirflow
的总和。在DataGrid中更改HeatingAirflow
或MinAirflow
参数的值时,使FanAirflow
参数的值自动更新的最佳方法是什么?
我已经研究过使用计算器方法,但是当我最终创建一个新的ParameterViewModel
并替换Parameters
集合中的“旧”虚拟机时,该实现感觉很笨拙。另外,FPBModel类中有许多计算值供参考,这只是最简单的示例。理想情况下,可以使用PropertyChanged事件,但是由于某种原因,我无法在嵌套类中到达那里。
FPBModel
public class EngineFPB : EngineMechanicalEquipment
{
private Parameter _minAirflow;
private Parameter _fanAirflow;
private Parameter _heatingAirflow;
public Parameter MinAirflow
{
get { return _minAirflow; }
set { _minAirflow = value; }
}
public eParameter FanAirflow
{
get { return _fanAirflow; }
set { _fanAirflow = value; }
}
public Parameter HeatingAirflow
{
get => _heatingAirflow;
//set => _heatingAirflow = _minAirflow + _fanAirflow;
}
public FPB(Element element)
{
UTILUnitConverter UC = new UTILUnitConverter();
double min = element.min;
double fan = element.fan;
_minAirflow = new EngineParameter("Min Airflow", min, false);
_fanAirflow = new EngineParameter("Fan Airflow", fan, false);
_heatingAirflow = new EngineParameter("Heating Airflow", 0, false);
CalculateFields();
}
public void CalculateFields()
{
double heatingAirflow = Convert.ToDouble(_minAirflow.Value) + Convert.ToDouble(_fanAirflow.Value);
_heatingAirflow.Value = heatingAirflow;
}
}
FPBViewModel
public class FPBViewModel : ViewModelBase
{
private ObservableCollection<ParameterViewModel> _parameters = new ObservableCollection<ParameterViewModel>();
public ObservableCollection<ParameterViewModel> Parameters
{
get => _parameters;
set
{
_parameters = value;
RaiseProperty(nameof(Parameters));
}
}
private ParameterViewModel _minAirflow;
public ParameterViewModel MinAirflow
{
get => _minAirflow;
set
{
_minAirflow = value;
RaiseProperty(nameof(MinAirflow));
}
}
private ParameterViewModel _fanAirflow;
public ParameterViewModel FanAirflow
{
get => _fanAirflow;
set
{
_fanAirflow = value;
RaiseProperty(nameof(FanAirflow));
}
}
private ParameterViewModel _heatingAirflow;
public ParameterViewModel HeatingAirflow
{
get => _heatingAirflow;
set
{
_heatingAirflow = value;
RaiseProperty(nameof(HeatingAirflow));
}
}
public FPBViewModel(EngineFPB fpb)
{
_minAirflow = new ParameterViewModel(fpb.MinAirflow, false);
_fanAirflow = new ParameterViewModel(fpb.FanAirflow, false);
_heatingAirflow = new ParameterViewModel(fpb.HeatingAirflow, true);
_parameters.Add(MinAirflow);
_parameters.Add(FanAirflow);
_parameters.Add(HeatingAirflow);
}
}
参数类
public class EngineParameter
{
private string _name;
private object _value;
private bool _isReadOnly;
public string Name
{
get => _name;
set => _name = value;
}
public object Value
{
get => _value;
set => _value = value;
}
public bool IsReadOnly
{
get => _isReadOnly;
set => _isReadOnly = value;
}
public EngineParameter(string name, object value, bool isReadOnly)
{
_name = name;
_value = value;
_isReadOnly = isReadOnly;
}
}
ParameterViewModel
public class ParameterViewModel : ViewModelBase
{
private Parameter _parameter;
private bool _isReadOnly;
public bool IsReadOnly
{
get => _isReadOnly;
set => _isReadOnly = value;
}
public string Name
{
get => _parameter.Name;
}
public object Value
{
get => _parameter.Value;
set
{
if (!_isReadOnly)
{
_parameter.Value = value;
RaiseProperty(nameof(Value));
}
}
}
public ParameterViewModel(Parameter parameter, bool isReadOnly)
{
_parameter = parameter;
_isReadOnly = isReadOnly;
}
}