问题描述
我有一个包含四列的数据网格:
Datagrid通过可观察的集合填充。查看模型是:
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;组; }
public double B {get;组; }
public double C {get;组; }
public double D {get;组; }
}
ObservableCollection< PlanningResult> populatePatternData = new ObservableCollection< PlanningResult>();
public ObservableCollection< PlanningResult> PopulatePatternData
{
get {return populatePatternData; }
set
{
populatePatternData = value;
base.OnPropertyChanged(StringList);
}
}
这是我的要求:
- 列C
和列D
应该具有相同的值,除非用户在列A中进行更改
或列B
。在这种情况下,列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;}
}
-
我遇到的问题是我无法使用
initial values = values same to columnC
填充columnD。如何向列D提供初始值? -
当我尝试通过集合来做,我得到一个错误:
属性D不能分配给它 - 只读
将一个setter添加到 D
属性中,并将其值设置为 A
和 B
属性:
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;组; }
}
I have a datagrid with four columns:
Datagrid is populated via an observable collection. View model is:
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");
}
}
Here is the requirement for me:- Column C
and Column D
should have same values unless user makes a change in Column A
or Column B
. In that case, Column D
should become ColumnA+ColumnB
.
Based on the above condition, I can make changes in the view model as
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;}
}
The issue that I face is that I am unable to populate columnD with "
initial values = values same as columnC
". How do I provide initial values to the column D?When I try to do it via collection, I get an error as :
property D cannot be assigned to -- it is read only
Add a setter to the D
property and set its value in the setter of the A
and B
properties:
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; }
}
这篇关于Datagrid错误 - 无法填充Viemodel属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!