我有一个包含四列的数据网格:
Datagrid通过可观察的集合填充。查看模型是:
+---------+--------------+-------------+------------+------------+-----------+
| bill_no | s_f_2000ml | s_f_1000ml | m_w_2000ml | m_w_1000ml | bill_date |
+---------+------------- + ------------+------------+------------+-----------+
| 2001 | 2 | 1 | 3 | 4 |01-02-2017 |
+---------+--------------+-------------+------------+------------+-----------+
这是我的要求:
- public class PlanningResult : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public double A { get; set; }
public double B { get; set; }
public double C { get; set; }
public double D { get; set; }
}
ObservableCollection<PlanningResult> populatePatternData = new ObservableCollection<PlanningResult>();
public ObservableCollection<PlanningResult> PopulatePatternData
{
get { return populatePatternData; }
set
{
populatePatternData = value;
base.OnPropertyChanged("StringList");
}
}
和Column C
应具有相同的值,除非用户在Column D
或Column A
中进行了更改。在这种情况下,Column B
应该成为Column D
。
基于上述条件,我可以在视图模型中进行更改
ColumnA+ColumnB
我遇到的问题是我无法使用“public class PlanningResult : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private double _a;
public double A
{
get { return _a; }
set { _a = value; NotifyPropertyChanged(); NotifyPropertyChanged("D"); }
}
private double _b;
public double B
{
get { return _b; }
set { _b = value; NotifyPropertyChanged(); NotifyPropertyChanged("D"); }
}
public double D { get { return _a + _b; } }
public double C {get;set;}
}
”填充columnD。如何为列D提供初始值?
当我尝试通过收集时,我收到一个错误:
initial values = values same as columnC
答案 0 :(得分:1)
在D
属性中添加一个setter,并在A
和B
属性的setter中设置其值:
public class PlanningResult : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
private double _a;
public double A
{
get { return _a; }
set { _a = value; NotifyPropertyChanged(); D = _a + _b; }
}
private double _b;
public double B
{
get { return _b; }
set { _b = value; NotifyPropertyChanged(); D = _a + _b; }
}
private double _d;
public double D
{
get { return _d; }
set { _d = value; NotifyPropertyChanged(); }
}
public double C { get; set; }
}
答案 1 :(得分:1)
您可以使用其他字段来存储A
或B
是否已更改。
private bool hasAorBChanged = false;
A
或B
更改后,此字段可设为true。
private double _a;
public double A
{
get { return _a; }
set { _a = value; NotifyPropertyChanged(); NotifyPropertyChanged("D");
hasAorBChanged = true;}
}
private double _b;
public double B
{
get { return _b; }
set { _b = value; NotifyPropertyChanged(); NotifyPropertyChanged("D");
hasAorBChanged = true;}
}
然后属性D看起来像
public double D { get { return hasAorBChanged ? _a + _b : C; } }
不是最干净的解决方案,但考虑到D是只读的,这应该可行。
修改:您也可以将hasAorBChanged = true;
移至方法NotifyPropertyChanged()
。它会让它变得更加清洁。