I have a simple app that stores different stock data, my add stock button opens a new window with a form and when I press save I add the stock to the database but the datagrid doesn't get updated
I access the static instance of my main Viewmodel from my EntryViewModel via:
Main View Model(StockViewModel)
public class StockViewModel:INotifyPropertyChanged
{
#region Fields
private IList<Stock> _stockList;
private StatsBuilder _statsBuilder = new StatsBuilder();
public Stock stock = new Stock();
public Stock10 stock10 = new Stock10();
public Stock20 stock20 = new Stock20();
private ICommand _searchCommand;
private static StockViewModel _instance = new StockViewModel();
public static StockViewModel Instance { get { return _instance; } }
public IList<Stock> Stocks
{
get { return _stockList; }
set { _stockList = value; OnPropertyChanged(nameof(Stocks)); }
}
#endregion
#region Constructor
public StockViewModel()
{
using (var stocks = new AppDbContext())
{
Stocks = stocks.LowFloatStocks.ToList();
}
}
#endregion
#region Day1's
private string _ticker;
public string Ticker
{
get { return _ticker; }
set
{
_ticker = value;
OnPropertyChanged("Ticker");
}
}
private DateTime _date = DateTime.Now;
public DateTime Date
{
get { return _date; }
set { _date = value; }
}
private decimal _preivousClose;
public decimal PreviousClose
{
get { return _preivousClose; }
set { _preivousClose = value; OnPropertyChanged("PreviousClose"); }
}
private decimal _pmOpeningPrice;
public decimal PM_OpeningPrice
{
get { return _pmOpeningPrice; }
set { _pmOpeningPrice = value; OnPropertyChanged("PM_OpeningPrice"); }
}
private decimal _openingPrice;
public decimal OpeningPrice
{
get { return _openingPrice; }
set { _openingPrice = value; OnPropertyChanged("OpeningPrice"); }
}
private decimal _high;
public decimal High
{
get { return _high ; }
set { _high = value; OnPropertyChanged("High"); }
}
private decimal _low;
public decimal Low
{
get { return _low; }
set { _low = value; OnPropertyChanged("Low"); }
}
private decimal _close;
public decimal Close
{
get { return _close; }
set { _close = value; OnPropertyChanged("Close"); }
}
private string _catalyst;
public string Catalyst
{
get { return _catalyst; }
set { _catalyst = value; OnPropertyChanged("Catalyst"); }
}
private decimal _float;
public decimal Float
{
get { return _float; }
set {_float = value; OnPropertyChanged("Float"); }
}
private string _dilution;
public string Dilution
{
get { return _dilution; }
set { _dilution = value; OnPropertyChanged("Dilution"); }
}
#endregion
#region Day2's
//public decimal Day2Open
//{
// //get { return stock.Day2Open; }
// set { stock.Day2Open = value; OnPropertyChanged("Day2Open"); }
//}
//public decimal Day2High
//{
// get { return stock.Day2High; }
// set { stock.Day2High = value; OnPropertyChanged("Day2High"); }
//}
//public decimal Day2Low
//{
// get { return stock.Day2Low; }
// set { stock.Day2Low = value; OnPropertyChanged("Day2Low"); }
//}
//public decimal Day2Close
//{
// get { return stock.Day2Close; }
// set { stock.Day2Close = value; OnPropertyChanged("Day2Close"); }
//}
#endregion
#region Stats
private Stock _selectedstock;
public Stock SelectedStock
{
private get
{
return _selectedstock;
}
set
{
_selectedstock = value;
OnPropertyChanged("SelectedStock");
GetStats(_selectedstock.Ticker);
}
}
public int Gaps10 { get { return stock10.Gaps10; } set { stock10.Gaps10 = value; OnPropertyChanged("Gaps10"); } }
public decimal AvgGap10 { get {return stock10.AvgGap10; } set { stock10.AvgGap10 = value; OnPropertyChanged("AvgGap10"); } }
public int CloseGreen10 { get { return stock10.CloseGreen10; } set { stock10.CloseGreen10 = value; OnPropertyChanged("CloseGreen10"); } }
public decimal CloseGreenPercent10 { get { return stock10.CloseGreenPercent10; } set { stock10.CloseGreenPercent10 = value; OnPropertyChanged("CloseGreenPercent10"); } }
public int CloseRed10 { get { return stock10.CloseRed10; } set { stock10.CloseRed10 = value; OnPropertyChanged("CloseRed10"); } }
public decimal CloseRedPercent10 { get { return stock10.CloseRedPercent10; } set { stock10.CloseRedPercent10 = value; OnPropertyChanged("CloseRedPercent10"); } }
public decimal AvgSpike10 { get; set; }
public decimal Downside10 { get; set; }
public int PMFade10 { get { return stock10.PMFade10; } set { stock10.PMFade10 = value; OnPropertyChanged("PMFade10"); } }
public decimal PMFadePercent10 { get { return stock10.PMFadePercent10; } set { stock10.PMFadePercent10 = value; OnPropertyChanged("PMFadePercent10"); } }
public int Gaps20 { get {return stock20.Gaps20 ; } set { stock20.Gaps20 = value; OnPropertyChanged("Gaps20"); } }
public decimal AvgGap20 { get { return stock20.AvgGap20; } set { stock20.AvgGap20 = value; OnPropertyChanged("AvgGap20"); } }
public int CloseGreen20 { get { return stock20.CloseGreen20; } set { stock20.CloseGreen20 = value; OnPropertyChanged("CloseGreen20"); } }
public decimal CloseGreenPercent20 { get { return stock20.CloseGreenPercent20; } set { stock20.CloseGreenPercent20 = value; OnPropertyChanged("CloseGreenPercent20"); } }
public int CloseRed20 { get { return stock20.CloseRed20; } set { stock20.CloseRed20 = value; OnPropertyChanged("CloseRed20"); } }
public decimal CloseRedPercent20 { get { return stock20.CloseRedPercent20; } set { stock20.CloseRedPercent20 = value; OnPropertyChanged("CloseRedPercent20"); } }
public decimal AvgSpike20 { get; set; }
public decimal Downside20 { get; set; }
public int PMFade20 { get { return stock20.PMFade20; } set { stock20.PMFade20 = value; OnPropertyChanged("PMFade20"); } }
public decimal PMFadePercent20 { get { return stock20.PMFadePercent20; } set { stock20.PMFadePercent20 = value; OnPropertyChanged("PMFadePercent20"); } }
#endregion
#region PropertyChange
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region Commands
public ICommand SearchCommand => _searchCommand ?? (_searchCommand = new RelayCommand(param => this.SearchStock(param)));
#endregion
#region Actions
private void SearchStock(object _ticker)
{
try
{
var stock = Stocks.First(x => x.Ticker.Trim() == _ticker.ToString());
_selectedstock = (Stock)stock;
GetStats(_ticker.ToString());
}
catch (Exception)
{
MessageBox.Show("No matching Ticker");
}
}
private void GetStats(string ticker)
{
//Stocks with 10% gap ups
stock10 = _statsBuilder.GetStats10(ticker);
stock20 = _statsBuilder.GetStats20(ticker);
Gaps10 = stock10.Gaps10;
AvgGap10 = stock10.AvgGap10;
CloseGreen10 = stock10.CloseGreen10;
CloseGreenPercent10 = stock10.CloseGreenPercent10;
CloseRed10 = stock10.CloseRed10;
CloseRedPercent10 = stock10.CloseRedPercent10;
PMFade10 = stock10.PMFade10;
PMFadePercent10 = stock10.PMFadePercent10;
//Stock with 20% gap ups
Gaps20 = stock20.Gaps20;
AvgGap20 = stock20.AvgGap20;
CloseGreen20 = stock20.CloseGreen20;
CloseGreenPercent20 = stock20.CloseGreenPercent20;
CloseRed20 = stock20.CloseRed20;
CloseRedPercent20 = stock20.CloseRedPercent20;
PMFade20 = stock20.PMFade20;
PMFadePercent20 = stock20.PMFadePercent20;
}
#endregion
}
When I click the add stock button it fires the AddCommand which in turn fires the Addstock method, this adds the stock to the database and updates the list in the StockViewModel instance but it does not update the datagrid OnPropertyChanged for some reason
EntryViewModel:
public ICommand AddCommand => _addCommand ?? (_addCommand = new RelayCommand(param => this.AddStock()));
#endregion
#region PropertyChange
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region Methods
private void AddStock()
{
using (var stocks = new AppDbContext())
{
stock.Id = 0;
stock.Ticker = _ticker;
stock.PreviousClose = _preivousClose;
stock.PM_OpeningPrice = _pmOpeningPrice;
stock.OpeningPrice = _openingPrice;
stock.High = _high;
stock.Low = _low;
stock.Close = _close;
stock.Catalyst = _catalyst;
stock.Float = _float;
stock.Dilution = _dilution;
stocks.LowFloatStocks.Add(stock);
stocks.SaveChanges();
StockViewModel.Instance.Stocks = stocks.LowFloatStocks.ToList();
Clear();
}
}
Data Grid Binding;
<DataGrid Name="StockGrid" Margin="5,5,5,5" Height="250" ItemsSource="{Binding Stocks}" IsReadOnly="True" AutoGeneratingColumn ="StockGrid_AutoGeneratingColumn" Grid.ColumnSpan="3" VerticalScrollBarVisibility="Auto" SelectedItem="{Binding SelectedStock, Mode=TwoWay}">
</DataGrid>
Data Context of StockView Model:
<Window.DataContext> <local1:StockViewModel/> </Window.DataContext>
Any suggestions?
The following sets the DataContext to an new instance of the StockViewModel which is not the same as the instance returned by the Instance property:
<Window.DataContext> <local1:StockViewModel/> </Window.DataContext>
The fact that you can do this means that StockViewModel is not really a singleton. You should add a private constuctor to it to make it a real singleton if this is what you want:
private StockViewModel() { }
You would then set the DataContext to the instance returned by the Instance property:
<Window.DataContext>
<x:Static Member="this:StockViewModel.Instance" />
</Window.DataContext>
Related
I have 2 classes: ParentClass and ChildClass and both are used as datacontext for UserControl with similar hierarchy.
I want to have parent class be notified when it's custom class property has a change in one of it's variables.
public class ParentClass
{
private ChildClass _child;
private int _value;
public ChildClass Child
{
get
{
if (_child == null)
Child = new ChildClass();
return _child;
}
set
{
_child = value;
Value = _child.Score;
OnPropertyChanged(nameof(Child));
}
}
public int Value
{
get { return _value; }
set
{
_value = value;
OnPropertyChanged(nameof(Value));
}
}
}
public class ChildClass
{
private int _score;
public int Score
{
get { return _score; }
set
{
_score = value;
OnPropertyChanged(nameof(Score));
}
}
}
What I want is that on change of Score, set part of Parent class's Child property is executed and Value updated.
How do I do that?
Edit: Code
PropertyChangedNotify
internal class PropertyChangedNotify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(string? property)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
Proficiency_Counter - xaml
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text=""/>
<Viewbox Grid.Column="1">
<ComboBox ItemsSource="{Binding ProficiencyList}" SelectedItem="{Binding Proficiency}" Margin="1">
<ComboBox.ItemTemplate>
<DataTemplate>
<Viewbox>
<TextBlock Text="{Binding}"/>
</Viewbox>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Viewbox>
</Grid>
Proficiency_Counter_Model - aka ChildClass
internal class Proficiency_Counter_Model: PropertyChangedNotify
{
#region private
private int _proficiencyScore;
private List<char> _proficiencyList;
private char _proficiency;
private int _level;
#endregion
#region public
public int ProficiencyScore
{
get { return _proficiencyScore; }
set
{
_proficiencyScore = value;
OnPropertyChanged(nameof(ProficiencyScore));
}
}
public List<char> ProficiencyList
{
get
{
if (_proficiencyList == null)
{
ProficiencyList = new List<char>();
ProficiencyList.Add('U');
ProficiencyList.Add('T');
ProficiencyList.Add('E');
ProficiencyList.Add('M');
ProficiencyList.Add('L');
Proficiency = 'U';
}
return _proficiencyList;
}
set
{
_proficiencyList = value;
OnPropertyChanged(nameof(ProficiencyList));
}
}
public char Proficiency
{
get { return _proficiency; }
set
{
_proficiency = value;
if (Proficiency == 'U')
{
ProficiencyScore = 0;
}
else if (Proficiency == 'T')
{
ProficiencyScore = 2 + _level;
}
else if (Proficiency == 'E')
{
ProficiencyScore = 4 + _level;
}
else if (Proficiency == 'M')
{
ProficiencyScore = 6 + _level;
}
else if (Proficiency == 'L')
{
ProficiencyScore = 8 + _level;
}
OnPropertyChanged(nameof(Proficiency));
}
}
public int Level
{
get { return _level; }
set
{
_level = value;
Proficiency = _proficiency;
OnPropertyChanged(nameof(Level));
}
}
#endregion
}
General_Skill_Counter_Model - aka ParentClass
internal class General_Skill_Counter_Model: PropertyChangedNotify
{
#region private
private string _skillName;
private int _abilityMod;
private Proficiency_Counter_Model _proficiency;
private int _proficiencyMod;
private int _itemMod;
#endregion
#region public
public string SkillName
{
get
{
if (_skillName == null)
SkillName = "Lorem Impsum";
return _skillName;
}
set
{
_skillName = value;
OnPropertyChanged(nameof(SkillName));
}
}
public int SkillScore
{
get
{
return (AbilityMod + Proficiency.ProficiencyScore + ItemMod);
}
}
public int AbilityMod
{
get { return _abilityMod; }
set
{
_abilityMod = value;
OnPropertyChanged(nameof(AbilityMod));
OnPropertyChanged(nameof(SkillScore));
}
}
public Proficiency_Counter_Model Proficiency
{
get
{
if (_proficiency == null)
{
_proficiency = new Proficiency_Counter_Model();
}
return _proficiency;
}
set
{
_proficiency = value;
ProficiencyMod = _proficiency.ProficiencyScore;
OnPropertyChanged(nameof(Proficiency));
}
}
public int ProficiencyMod
{
get { return _proficiencyMod; }
set
{
_proficiencyMod = value;
OnPropertyChanged(nameof(ProficiencyMod));
OnPropertyChanged(nameof(SkillScore));
}
}
public int ItemMod
{
get { return _itemMod; }
set
{
_itemMod = value;
OnPropertyChanged(nameof(ItemMod));
OnPropertyChanged(nameof(SkillScore));
}
}
#endregion
}
Proficiency_Counter_Model is set as Proficiency_Counter's DataContext, same for Genereal_Skill_Counter, which I believe is irrelevant to this.
Ok, I figured it out. Instead of notifying the parent, I made the child part of the parent.
Sadly, this only works for single class - It would not work if I had 2 different children of the same class.
Here's the code:
internal class Proficiency_Counter_Model: PropertyChangedNotify
{
#region private
protected int _proficiencyScore;
private List<char> _proficiencyList;
private char _proficiency;
protected int _level;
#endregion
#region public
public virtual int ProficiencyScore
{
get { return _proficiencyScore; }
set
{
_proficiencyScore = value;
OnPropertyChanged(nameof(ProficiencyScore));
}
}
public List<char> ProficiencyList
{
get
{
if (_proficiencyList == null)
{
ProficiencyList = new List<char>();
ProficiencyList.Add('U');
ProficiencyList.Add('T');
ProficiencyList.Add('E');
ProficiencyList.Add('M');
ProficiencyList.Add('L');
Proficiency = 'U';
}
return _proficiencyList;
}
set
{
_proficiencyList = value;
OnPropertyChanged(nameof(ProficiencyList));
}
}
public char Proficiency
{
get { return _proficiency; }
set
{
_proficiency = value;
if (Proficiency == 'U')
{
ProficiencyScore = 0;
}
else if (Proficiency == 'T')
{
ProficiencyScore = 2 + _level;
}
else if (Proficiency == 'E')
{
ProficiencyScore = 4 + _level;
}
else if (Proficiency == 'M')
{
ProficiencyScore = 6 + _level;
}
else if (Proficiency == 'L')
{
ProficiencyScore = 8 + _level;
}
OnPropertyChanged(nameof(Proficiency));
}
}
public int Level
{
get { return _level; }
set
{
_level = value;
Proficiency = _proficiency;
OnPropertyChanged(nameof(Level));
}
}
#endregion
}
I made the ProficiencyScore property virtual so that I could override it.
internal class General_Skill_Counter_Model : Proficiency_Counter_Model
{
#region private
private string _skillName;
private int _skillScore;
private int _abilityMod;
private int _itemMod;
#endregion
#region public
public string SkillName
{
get
{
if (_skillName == null)
SkillName = "Lorem Impsum";
return _skillName;
}
set
{
_skillName = value;
OnPropertyChanged(nameof(SkillName));
}
}
public int SkillScore
{
get { return _skillScore; }
set
{
_skillScore = value;
OnPropertyChanged(nameof(SkillScore));
}
}
public int AbilityMod
{
get { return _abilityMod; }
set
{
_abilityMod = value;
OnPropertyChanged(nameof(AbilityMod));
}
}
public int ItemMod
{
get { return _itemMod; }
set
{
_itemMod = value;
OnPropertyChanged(nameof(ItemMod));
}
}
#endregion
#region override
public override int ProficiencyScore
{
get { return _proficiencyScore; }
set
{
SkillScore = _skillScore + value - _proficiencyScore;
_proficiencyScore = value;
OnPropertyChanged(nameof(ProficiencyScore));
}
}
#endregion
}
I am trying to Serialize/Deserialize a object, but I am getting an "ArgumentException" on deserialization.
My Object is this:
public class Findoc
{
public Findoc()
{
}
private string _ID = string.Empty;
public string ID
{
get { return this._ID; }
set { _ID = value; }
}
private int _lastindex;
public int lastindex
{
get { return this._lastindex; }
set { _lastindex = value; }
}
private string _SilogiDate;
public string SilogiDate
{
get { return this._SilogiDate; }
set { _SilogiDate = value; }
}
private Truck _TR;
public Truck TR
{
get { return this._TR; }
set { _TR = value; }
}
private Routing _RT;
public Routing RT
{
get { return this._RT; }
set { _RT = value; }
}
private KentroKostous _KK;
public KentroKostous KK
{
get { return this._KK; }
set { _KK = value; }
}
private Busunit _BU;
public Busunit BU
{
get { return this._BU; }
set { _BU = value; }
}
private string _FINCODE = string.Empty;
public string FINCODE
{
get { return this._FINCODE; }
set { _FINCODE = value; }
}
private string _FINSTATE = "";
public string FINSTATE
{
get { return this._FINSTATE; }
set { _FINSTATE = value; }
}
private string _STAGE = "";
public string STAGE
{
get { return this._STAGE; }
set { _STAGE = value; }
}
private string _SPCS = "";
public string SPCS
{
get { return this._SPCS; }
set { _SPCS = value; }
}
private string _SPCSCODE = "";
public string SPCSCODE
{
get { return this._SPCSCODE; }
set { _SPCSCODE = value; }
}
private string _MTRSTS = "";
public string MTRSTS
{
get { return this._MTRSTS; }
set { _MTRSTS = value; }
}
private string _PARAGOMENO = "";
public string PARAGOMENO
{
get { return this._PARAGOMENO; }
set { _PARAGOMENO = value; }
}
private double _PARAGOMENOQTY1;
public double PARAGOMENOQTY1
{
get { return this._PARAGOMENOQTY1; }
set { _PARAGOMENOQTY1 = value; }
}
private double _PARAGOMENOQTY2;
public double PARAGOMENOQTY2
{
get { return this._PARAGOMENOQTY2; }
set { _PARAGOMENOQTY2 = value; }
}
private Boolean _PARAGOMENOUSESN;
public Boolean PARAGOMENOUSESN
{
get { return this._PARAGOMENOUSESN; }
set { _PARAGOMENOUSESN = value; }
}
private Boolean _EDITABLE = true;
public Boolean EDITABLE
{
get { return this._EDITABLE; }
set { _EDITABLE = value; }
}
private Boolean _ISPRINT;
public Boolean ISPRINT
{
get { return this._ISPRINT; }
set { _ISPRINT = value; }
}
private Boolean _ISCANCELED;
public Boolean ISCANCELED
{
get { return this._ISCANCELED; }
set { _ISCANCELED = value; }
}
private int _SOSOURCE;
public int SOSOURCE
{
get { return this._SOSOURCE; }
set { _SOSOURCE = value; }
}
private Series _series;
public Series Series
{
get { return this._series; }
set { _series = value; }
}
private string _TICK = string.Empty;
public string TICK
{
get { return this._TICK; }
set { _TICK = value; }
}
private string _COMMENTS = "";
public string COMMEMTS
{
get { return this._COMMENTS; }
set { _COMMENTS = value; }
}
private string _COMMENTS1 = "";
public string COMMEMTS1
{
get { return this._COMMENTS1; }
set { _COMMENTS1 = value; }
}
private string _COMMENTS2 = "";
public string COMMEMTS2
{
get { return this._COMMENTS2; }
set { _COMMENTS2 = value; }
}
private string _karfoto_fincode = "";//gia tis eisprakseis
public string Karfoto_fincode
{
get { return this._karfoto_fincode; }
set { _karfoto_fincode = value; }
}
private List<Mtrline> _Mtrlines;
public List<Mtrline> Mtrlines
{
get { return this._Mtrlines; }
set { _Mtrlines = value; }
}
}
public class SnLine
{
private string _TICK;
public string TICK
{
get { return this._TICK; }
set { _TICK = value; }
}
private string _sn;
public string sn
{
get { return this._sn; }
set { _sn = value; }
}
}
public class Mtrline
{
public Mtrline(Findoc findoc)
{
}
private List<SnLine> _SnLines;
public List<SnLine> SnLines
{
get { return this._SnLines; }
set { _SnLines = value; }
}
private int _position;
public int position
{
get { return this._position; }
set { _position = value; }
}
private string _guid;
public string guid
{
get { return this._guid; }
set { _guid = value; }
}
private WhouseObj _Whouse1;
public WhouseObj Whouse1
{
get { return this._Whouse1; }
set { _Whouse1 = value; }
}
private WhouseObj _Whouse2;
public WhouseObj Whouse2
{
get { return this._Whouse2; }
set { _Whouse2 = value; }
}
private string _WHOUSE = "";
public string WHOUSE
{
get { return this._WHOUSE; }
set { _WHOUSE = value; }
}
private string _WHOUSESEC = "";
public string WHOUSESEC
{
get { return this._WHOUSESEC; }
set { _WHOUSESEC = value; }
}
private string _SPCS;
public string SPCS
{
get { return this._SPCS; }
set { _SPCS = value; }
}
private string _MPKCODE;
public string MPKCODE
{
get { return this._MPKCODE; }
set { _MPKCODE = value; }
}
private string _whousebin1remain;
public string Whousebin1remain
{
get { return this._whousebin1remain; }
set { _whousebin1remain = value; }
}
private string _whousebin2remain;
public string Whousebin2remain
{
get { return this._whousebin2remain; }
set { _whousebin2remain = value; }
}
private int _NEWLINE;
public int NEWLINE
{
get { return this._NEWLINE; }
set { _NEWLINE = value; }
}
private string _CCCPACK1;
public string CCCPACK1
{
get {
if (_CCCPACK1 == null)
return "";
else
return this._CCCPACK1;
}
set { _CCCPACK1 = value; }
}
public string _CCCPACK2;
public string CCCPACK2
{
get
{
if (_CCCPACK2 == null)
return "";
else
return this._CCCPACK2;
}
set { _CCCPACK2 = value; }
}
private string _PAKETO = "";
public string PAKETO
{
get { return this._PAKETO; }
set { _PAKETO = value; }
}
private string _TICK;
public string TICK
{
get { return this._TICK; }
set { _TICK = value; }
}
private string _AAA;
public string AAA
{
get { return this._AAA; }
set { _AAA = value; }
}
private MtrlModel _MTRL_Object;
public MtrlModel MTRL_Object
{
get { return this._MTRL_Object; }
set { _MTRL_Object = value; }
}
private double _SUMQTY; // SUMQTY ana eidos
public double SUMQTY
{
get { return this._SUMQTY; }
set { _SUMQTY = value; }
}
private double _SUMQTY2; // SUMQTY ana eidos
public double SUMQTY2
{
get { return this._SUMQTY2; }
set { _SUMQTY2 = value; }
}
private double _QTY; // posotita T
public double QTY
{
get { return this._QTY; }
set { _QTY = value; }
}
private double _QTY1;
public double QTY1
{
get { return this._QTY1; }
set { _QTY1 = value; }
}
private double _QTY2;
public double QTY2
{
get { return this._QTY2; }
set { _QTY2 = value; }
}
private double _QTYP;
public double QTYP
{
get { return this._QTYP; }
set { _QTYP = value; }
}
private string _FIELDS = "";
public string FIELDS
{
get { return this._FIELDS; }
set { _FIELDS = value; }
}
private string _COMMENTS = "";
public string COMMENTS
{
get { return this._COMMENTS; }
set { _COMMENTS = value; }
}
private string _COMMENTS1 = "";
public string COMMENTS1
{
get { return this._COMMENTS1; }
set { _COMMENTS1 = value; }
}
private string _COMMENTS2 = "";
public string COMMENTS2
{
get { return this._COMMENTS2; }
set { _COMMENTS2 = value; }
}
private CDIMLINE _CDIMNO1_Object;
public CDIMLINE CDIMNO1_Object
{
get { return this._CDIMNO1_Object; }
set { _CDIMNO1_Object = value; }
}
private string _CDIMNO1;
public string CDIMNO1
{
get
{
if (this._CDIMNO1_Object != null)
return this._CDIMNO1_Object.NAME;
else
return "";
}
set { _CDIMNO1 = value; }
}
private CDIMLINE _CDIMNO2_Object;
public CDIMLINE CDIMNO2_Object
{
get { return this._CDIMNO2_Object; }
set { _CDIMNO2_Object = value; }
}
private string _CDIMNO2;
public string CDIMNO2
{
get
{
if (this._CDIMNO2_Object != null)
return this._CDIMNO2_Object.NAME;
else
return "";
}
set { _CDIMNO2 = value; }
}
private CDIMLINE _CDIMNO3_Object;
public CDIMLINE CDIMNO3_Object
{
get { return this._CDIMNO3_Object; }
set { _CDIMNO3_Object = value; }
}
private string _CDIMNO3;
public string CDIMNO3
{
get
{
if (this._CDIMNO3_Object != null)
return this._CDIMNO3_Object.NAME;
else
return "";
}
set { _CDIMNO3 = value; }
}
private Lot _LOT;
public Lot LOT
{
get { return this._LOT; }
set { _LOT = value; }
}
private string _CODE = "";
public string CODE
{
get { return this._MTRL_Object.CODE; }
set { _CODE = value; }
}
private string _MTRPLACE = "";
public string MTRPLACE
{
get { return this._MTRL_Object.MTRPLACE; }
set { _MTRPLACE = value; }
}
private double _SUMWHOUSE;
public double SUMWHOUSE
{
get { return this._MTRL_Object.REMAIN; }
set { _SUMWHOUSE = value; }
}
private double _WHOUSE_SERIES;
public double WHOUSE_SERIES
{
get { return this._MTRL_Object.WHOUSE_SERIES_REMAIN; }
set { _WHOUSE_SERIES = value; }
}
private string _CODE1 = "";
public string CODE1
{
get { return this._MTRL_Object.CODE1; }
set { _CODE1 = value; }
}
private string _CODE2 = "";
public string CODE2
{
get { return this._MTRL_Object.CODE2; }
set { _CODE2 = value; }
}
private string _NAME = "";
public string NAME
{
get { return this._MTRL_Object.NAME; }
set { _NAME = value; }
}
private string _MTRL = "";
public string MTRL
{
get { return this._MTRL_Object.MTRL; }
set { _MTRL = value; }
}
private string _MTRUNIT1 = "";
public string MTRUNIT1
{
get
{
if (this._MTRL_Object.MTRUNIT1 != null)
return this._MTRL_Object.MTRUNIT1.name;
else
return "";
}
set { _MTRUNIT1 = value; }
}
private string _MTRUNIT2 = "";
public string MTRUNIT2
{
get
{
if (this._MTRL_Object.MTRUNIT2 != null)
return this._MTRL_Object.MTRUNIT2.name;
else
return "";
}
set { _MTRUNIT2 = value; }
}
private string _MTRUNIT3 = "";
public string MTRUNIT3
{
get
{
if (this._MTRL_Object.MTRUNIT3 != null)
return this._MTRL_Object.MTRUNIT3.name;
else
return "";
}
set { _MTRUNIT3 = value; }
}
private string _MTRUNIT4 = "";
public string MTRUNIT4
{
get
{
if (this._MTRL_Object.MTRUNIT4 != null)
return this._MTRL_Object.MTRUNIT4.name;
else
return "";
}
set { _MTRUNIT4 = value; }
}
private string _CRLOTCODE = "";
public string CRLOTCODE
{
get { return this._CRLOTCODE; }
set { _CRLOTCODE = value; }
}
private string _CRLOTCODE1 = "";
public string CRLOTCODE1
{
get { return this._CRLOTCODE1; }
set { _CRLOTCODE1 = value; }
}
private string _CRLOTCODE2 = "";
public string CRLOTCODE2
{
get { return this._CRLOTCODE2; }
set { _CRLOTCODE2 = value; }
}
private string _CRLOTFDATE = "";
public string CRLOTFDATE
{
get { return this._CRLOTFDATE; }
set { _CRLOTFDATE = value; }
}
private string _SCANEDCODE = "";
public string SCANEDCODE
{
get { return this._SCANEDCODE; }
set { _SCANEDCODE = value; }
}
private Thesi _WHOUSEBIN1_Object;
public Thesi WHOUSEBIN1_Object
{
get { return this._WHOUSEBIN1_Object; }
set { _WHOUSEBIN1_Object = value; }
}
private string _WHOUSEBIN1;
public string WHOUSEBIN1
{
get
{
if (this._WHOUSEBIN1_Object != null)
return this._WHOUSEBIN1_Object.name;
else
return "";
}
set { _WHOUSEBIN1 = value; }
}
private Thesi _WHOUSEBIN2_Object;
public Thesi WHOUSEBIN2_Object
{
get { return this._WHOUSEBIN2_Object; }
set { _WHOUSEBIN2_Object = value; }
}
private string _WHOUSEBIN2;
public string WHOUSEBIN2
{
get
{
if (this._WHOUSEBIN2_Object != null)
return this._WHOUSEBIN2_Object.name;
else
return "";
}
set { _WHOUSEBIN2 = value; }
}
private double _ANAMENOMENA;
public double ANAMENOMENA
{
get { return this._ANAMENOMENA; }
set { _ANAMENOMENA = value; }
}
private string _FINCODE = "";
public string FINCODE
{
get { return this._FINCODE; }
set { _FINCODE = value; }
}
private string _FINDOC = "";
public string FINDOC
{
get { return this._FINDOC; }
set { _FINDOC = value; }
}
private string _SODTYPE = "";
public string SODTYPE
{
get { return this._SODTYPE; }
set { _SODTYPE = value; }
}
private string _STATUS = "";
public string STATUS
{
get { return this._STATUS; }
set { _STATUS = value; }
}
private string _AA = "";
public string AA
{
get { return this._AA; }
set { _AA = value; }
}
private string _GUARANTY_SNCODE = "";
public string GUARANTY_SNCODE
{
get { return this._GUARANTY_SNCODE; }
set { _GUARANTY_SNCODE = value; }
}
}
I serialize it with the following code:
string data = Newtonsoft.Json.JsonConvert.SerializeObject(UniversalModel.Parastatiko,Formatting.None , new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
When I try to deserialize back to the object I get the exception error: ArgumentException.
The code to deserialize is this.
Findoc FFF = Newtonsoft.Json.JsonConvert.DeserializeObject<Findoc>(data, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Any tips on what I am doing wrong?
It may just be a typo in the question but separating the Serializer arguments on to their own lines shows you are passing a UniversalModel.Parastatiko in as the first argument...which I don't think is correct.
string data = Newtonsoft.Json.JsonConvert.SerializeObject(
UniversalModel.Parastatiko,
Formatting.None,
new JsonSerializerSettings {
NullValueHandling = NullValueHandling.Ignore
}
);
I have a MVVM program with a model:
public class Deelnemer
{
public int Id { get; set; }
public string Voornaam { get; set; }
public string Werkplek { get; set; }
public List<string> Aanwezig { get; set; }
public Deelnemer()
{
}
}
In my View I have a listBox in which I want to be able to select multiple values (days to put in the list aanwezig).
<ListBox Name="listDagdelen" SelectionMode="Multiple" ItemsSource="{Binding Dagdelen}" SelectedItem="{Binding SelectedDagdeel, Mode=TwoWay}">
The ViewModel looks as follows:
class DeelnemerViewModel : INotifyPropertyChanged
{
#region Private Variables
private readonly Deelnemer dlnObject;
private readonly ObservableCollection<Deelnemer> deelnemers;
private readonly DeelnemerManager deelnemerManager;
private readonly ICommand addDeelnemerCmd;
private readonly ICommand deleteDeelnemerCmd;
#endregion
public ObservableCollection<string> Dagdelen { get; private set; }
#region constructor
public DeelnemerViewModel()
{
Dagdelen = new ObservableCollection<string>() { "maandagochtend", "maandagmiddag", "dinsdagochtend", "dinsdagmiddag", "woensdagochtend", "woensdagmiddag", "donderdagochtend", "donderdagmiddag", "vrijdagochtend", "vrijdagmiddag" };
dlnObject = new Deelnemer();
deelnemerManager = new DeelnemerManager();
deelnemers = new ObservableCollection<Deelnemer>();
addDeelnemerCmd = new RelayCommand(Add, CanAdd);
deleteDeelnemerCmd = new RelayCommand(Delete, CanDelete);
}
#endregion
#region Properties
private string _selectedDagdeel = null;
public string SelectedDagdeel
{
get { return _selectedDagdeel; }
set
{
_selectedDagdeel = value;
dlnObject.Aanwezig.Add(value);
OnPropertyChanged("SelectedDagdeel");
}
}
public int Id
{
get { return dlnObject.Id; }
set
{
dlnObject.Id = value;
OnPropertyChanged("Id");
}
}
public string Voornaam
{
get { return dlnObject.Voornaam; }
set
{
dlnObject.Voornaam = value;
OnPropertyChanged("Voornaam");
}
}
public string Werkplek
{
get { return dlnObject.Werkplek; }
set
{
dlnObject.Werkplek = value;
OnPropertyChanged("Werkplek");
}
}
public List<string> Aanwezig
{
get { return dlnObject.Aanwezig; }
set
{
dlnObject.Aanwezig = value;
OnPropertyChanged("Aanwezig");
}
}
public ObservableCollection<Deelnemer> Deelnemers { get { return deelnemers; } }
public Deelnemer SelectedDeelnemer
{
set
{
Id = value.Id;
Voornaam = value.Voornaam;
Werkplek = value.Werkplek;
Aanwezig = value.Aanwezig;
}
}
#endregion
#region Commands
public ICommand AddDeelnemerCmd { get { return addDeelnemerCmd; } }
public ICommand DeleteDeelnemerCmd { get { return deleteDeelnemerCmd; } }
#endregion
public bool CanAdd(object obj)
{
//Enable the Button only if the mandatory fields are filled
if (Voornaam != string.Empty && Werkplek != string.Empty)
return true;
return false;
}
public void Add(object obj)
{
var deelnemer = new Deelnemer { Voornaam = Voornaam, Werkplek = Werkplek, Aanwezig = Aanwezig };
if (deelnemerManager.Add(deelnemer))
{
Deelnemers.Add(deelnemer);
//string txt = string.Join(String.Empty,Aanwezig);
//MessageBox.Show(txt);
//ResetDeelnemer();
}
else
MessageBox.Show("Vul correcte waardes in!");
}
#region DeleteCommand
private bool CanDelete(object obj)
{
//Enable the Button only if the patients exist
if (Deelnemers.Count > 0)
return true;
return false;
}
private void Delete(object obj)
{
//Delete patient will be successfull only if the patient with this ID exists.
if (!deelnemerManager.Remove(Id))
MessageBox.Show("Deelnemer met dit id bestaat niet!");
else
{
//Remove the patient from our collection as well.
deelnemers.RemoveAt(GetIndex(Id));
ResetDeelnemer();
MessageBox.Show("Deelnemer succesvol verwijderd !");
}
}
#endregion
#region Private Methods
private void ResetDeelnemer()
{
Id = 0;
Voornaam = string.Empty;
Werkplek = string.Empty;
Aanwezig.Clear();
}
private int GetIndex(int Id)
{
for (int i = 0; i < Deelnemers.Count; i++)
if (Deelnemers[i].Id == Id)
return i;
return -1;
}
#endregion
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
I can't figure out how I should use the List with the listbox values. How do I add (multiple) listbox values to the object's List?
The current code throws a nullreferenceexception at
dlnObject.Aanwezig.Add(value);
You must initialize the Aanwezig property of the Deelnemer object before you can add any values to it, either in the contructor of DeelnemerViewModel:
dlnObject = new Deelnemer();
dlnObject.Aanwezig = new List<string();
...or in the constructor of the Deeelnemer class:
public Deelnemer()
{
Aanwezig = new List<string();
}
I have a set of classes that I am using to deserialize JSON into. My program will periodically look for changes to this JSON file, and if it finds any, will push the new data to the properties of these classes using reflection.
I need to find any new items added to the collection of the Item2 class (SocialExportJSON.SocialExportData.Item2) after a successful update.
My JSON classes look like this (there are more but I want to avoid too big a wall of code):
public class Item2 : INotifyPropertyChanged
{
[JsonProperty("type")]
private string type;
public string Type
{
get
{
return type;
}
set
{
if (type != value)
{
type = value;
RaisePropertyChanged("Type");
}
}
}
[JsonProperty("id")]
private string id;
public string ID
{
get
{
return id;
}
set
{
if (id != value)
{
id = value;
RaisePropertyChanged("ID");
}
}
}
[JsonProperty("postedIso8601")]
private string postedIso8601;
public string PostedIso8601
{
get
{
return postedIso8601;
}
set
{
if (postedIso8601 != value)
{
postedIso8601 = value;
RaisePropertyChanged("PostedIso8601");
}
}
}
[JsonProperty("postedTimestamp")]
private object postedTimestamp;
public object PostedTimestamp
{
get
{
return postedTimestamp;
}
set
{
if (postedTimestamp != value)
{
postedTimestamp = value;
RaisePropertyChanged("PostedTimestamp");
}
}
}
[JsonProperty("engagement")]
private Engagement engagement;
public Engagement Engagement
{
get
{
return engagement;
}
set
{
if (engagement != value)
{
engagement = value;
RaisePropertyChanged("Engagement");
}
}
}
[JsonProperty("source")]
private Source2 source;
public Source2 Source
{
get
{
return source;
}
set
{
if (source != value)
{
source = value;
RaisePropertyChanged("Source");
}
}
}
[JsonProperty("author")]
private Author author;
public Author Author
{
get
{
return author;
}
set
{
if (author != value)
{
author = value;
RaisePropertyChanged("Author");
}
}
}
[JsonProperty("content")]
private Content content;
public Content Content
{
get
{
return content;
}
set
{
if (content != value)
{
content = value;
RaisePropertyChanged("Content");
}
}
}
[JsonProperty("location")]
private Location location;
public Location Location
{
get
{
return location;
}
set
{
if (location != value)
{
location = value;
RaisePropertyChanged("Location");
}
}
}
[JsonProperty("publication")]
private Publication publication;
public Publication Publication
{
get
{
return publication;
}
set
{
if (publication != value)
{
publication = value;
RaisePropertyChanged("Publication");
}
}
}
[JsonProperty("metadata")]
private Metadata metadata;
public Metadata Metadata
{
get
{
return metadata;
}
set
{
if (metadata != value)
{
metadata = value;
RaisePropertyChanged("Metadata");
}
}
}
//Event handling
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
//Console.WriteLine("Updated");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
public class SocialExportData : INotifyPropertyChanged
{
[JsonProperty("dataType")]
private string dataType;
public string DataType
{
get
{
return dataType;
}
set
{
if (dataType != value)
{
dataType = value;
RaisePropertyChanged("DataType");
}
}
}
[JsonProperty("id")]
private int id;
public int ID
{
get
{
return id;
}
set
{
if (id != value)
{
id = value;
RaisePropertyChanged("ID");
}
}
}
[JsonProperty("story")]
private Story story;
public Story Story
{
get
{
return story;
}
set
{
if (story != value)
{
story = value;
RaisePropertyChanged("Story");
}
}
}
[JsonProperty("order")]
private string order;
public string Order
{
get
{
return order;
}
set
{
if (order != value)
{
order = value;
RaisePropertyChanged("Order");
}
}
}
[JsonProperty("lifetime")]
private string lifetime;
public string Lifetime
{
get
{
return lifetime;
}
set
{
if (lifetime != value)
{
lifetime = value;
RaisePropertyChanged("Lifetime");
}
}
}
[JsonProperty("maxAge")]
private int maxAge;
public int MaxAge
{
get
{
return maxAge;
}
set
{
if (maxAge != value)
{
maxAge = value;
RaisePropertyChanged("MaxAge");
}
}
}
[JsonProperty("maxSize")]
private int maxSize;
public int MaxSize
{
get
{
return maxSize;
}
set
{
if (maxSize != value)
{
maxSize = value;
RaisePropertyChanged("MaxSize");
}
}
}
[JsonProperty("consumeCount")]
private int consumeCount;
public int ConsumeCount
{
get
{
return consumeCount;
}
set
{
if (consumeCount != value)
{
consumeCount = value;
RaisePropertyChanged("ConsumeCount");
}
}
}
[JsonProperty("consumeInterval")]
private int consumeInterval;
public int ConsumeInterval
{
get
{
return consumeInterval;
}
set
{
if (consumeInterval != value)
{
consumeInterval = value;
RaisePropertyChanged("ConsumeInterval");
}
}
}
[JsonProperty("items")]
private ObservableCollection<Item2> items;
public ObservableCollection<Item2> Items
{
get
{
return items;
}
set
{
if (items != value)
{
items = value;
RaisePropertyChanged("Items");
}
}
}
//Event handling
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
//Console.WriteLine("Updated");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
public class SocialExportJSON : INotifyPropertyChanged
{
[JsonProperty("id")]
private string id;
public string ID
{
get
{
return id;
}
set
{
if (id != value)
{
id = value;
RaisePropertyChanged("ID");
}
}
}
[JsonProperty("ttl")]
private int ttl;
public int TTL
{
get
{
return ttl;
}
set
{
if (ttl != value)
{
ttl = value;
RaisePropertyChanged("TTL");
}
}
}
[JsonProperty("serial")]
private long serial;
public long Serial
{
get
{
return serial;
}
set
{
if (serial != value)
{
serial = value;
RaisePropertyChanged("Serial");
}
}
}
[JsonProperty("formatType")]
private string formatType;
public string FormatType
{
get
{
return formatType;
}
set
{
if (formatType != value)
{
formatType = value;
RaisePropertyChanged("FormatType");
}
}
}
[JsonProperty("modifiedIso8601")]
private string modifiedIso8601;
public string ModifiedIso8601
{
get
{
return modifiedIso8601;
}
set
{
if (modifiedIso8601 != value)
{
modifiedIso8601 = value;
RaisePropertyChanged("ModifiedIso8601");
}
}
}
[JsonProperty("modifiedTimestamp")]
private long modifiedTimestamp;
public long ModifiedTimestamp
{
get
{
return modifiedTimestamp;
}
set
{
if (modifiedTimestamp != value)
{
modifiedTimestamp = value;
RaisePropertyChanged("ModifiedTimestamp");
}
}
}
[JsonProperty("timezone")]
private string timezone;
public string Timezone
{
get
{
return timezone;
}
set
{
if (timezone != value)
{
timezone = value;
RaisePropertyChanged("Timezone");
}
}
}
[JsonProperty("dataType")]
private string dataType;
public string DataType
{
get
{
return dataType;
}
set
{
if (dataType != value)
{
dataType = value;
RaisePropertyChanged("DataType");
}
}
}
[JsonProperty("exports")]
private ObservableCollection<SocialExportData> exports;
public ObservableCollection<SocialExportData> Exports
{
get
{
return exports;
}
set
{
if (exports != value)
{
exports = value;
RaisePropertyChanged("Exports");
}
}
}
//Event handling
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string property)
{
//Console.WriteLine("Updated");
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(property));
}
}
In another class, I have a method to deserialize to a global instance of my JSON class. It looks like this:
public SocialExportJSON socialExportData;
private async void DownloadAndDeserializeJSONAsync()
{
try
{
//Create a web client with the supplied credentials
var exportClient = new WebClient { Credentials = new NetworkCredential(uName, pw), Encoding = Encoding.UTF8};
//Create a task to download the JSON string and wait for it to finish
var downloadTask = Task.Run(() => exportClient.DownloadString(new Uri(eURL)));
downloadTask.Wait();
//Get the string from the task
var JSONString = await downloadTask;
//Create a task to deserialize the JSON from the last task
var DeserializeTask = Task.Run(() => JsonConvert.DeserializeObject<SocialExportJSON>(JSONString));
DeserializeTask.Wait();
SocialExportJSON sej = await DeserializeTask;
//Check the timestamp first to see if we should change the data
if(socialExportData == null)
{
//Get the data from the task
socialExportData = await DeserializeTask;
}
else if(sej.ModifiedTimestamp != socialExportData.ModifiedTimestamp)
{
//Get the data from the task
SocialExportJSON newData = await DeserializeTask;
GetNewItems(newData);
SetNewData(newData);
//Call the exportUpdated event when the task has finished
exportUpdated();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
}
}
In my SetNewData function, shown below, I use reflection to set the properties of my global class. Because I'm setting the whole collection rather than iterating through each of the properties in each of the classes, I can't use the CollectionChanged event to find new items.
public void SetNewData(SocialExportJSON newData)
{
//Loop through each of the properties and copy from source to target
foreach (PropertyInfo pi in socialExportData.GetType().GetProperties())
{
if (pi.CanWrite)
{
pi.SetValue(socialExportData, pi.GetValue(newData, null), null);
}
}
}
Is there a way I can modify my SetNewData function in such a way that it calls CollectionChanged? If not, what would be the best way to go about getting any new additions to my collection of Item2?
In my Main function. I create an instance of my class called SocialExport like so:
SocialExport s = new SocialExport("http://example.json", "example", "example");.
This class is where the global instance of my JSON class is contained, and my event handler is added like so
s.socialExportData.Exports[0].Items.CollectionChanged += CollectionChanged;
Then you are hooking up an event handler for the CollectionChanged event for that particular instance of ObservableCollection<Item2>.
If you create a new ObservableCollection<Item2>, you obviously must hook up an event handler to this one as well. The event handler that is associated with the old object won't be invoked when new items are added to the new instance.
So whenever a new ObservableCollection<Item2> is created, using deserialization or not, you should hook up a new event handler.
You could probably do this in your DownloadAndDeserializeJSONAsync method. The other option would be to create only one instance of the collection and remove and add items from/to this one.
I populate a data grid with a list of objects that come from a repository like this:
public static IEnumerable<Order> GetOrdersForDataGrid()
{
IEnumerable<Order> query;
using (RSDContext = new RSDContext())
{
query = context.Orders.Include(o=>o.OrderDetails).ToList();
}
return query;
}
When I want to edit an order I pass the selected row to a new window like this:
OrderEditWindow orderEdit = new OrderEditWindow();
orderEdit.SelectedOrder = SelectedOrder;
orderEdit.ShowDialog();
Here I set the DataContext of the Window to:
DataContext = SelectedOrder;
In this window I have another data grid that binds to OrderDetails collection property of Order. The problem is on CRUD operations on OrderDetails. For example, after I add a new orderDetail like this:
private void AddProductDetailButton_OnClick(object sender, RoutedEventArgs e)
{
if (!ValidateProductDetail())
return;
var _selectedProduct = ProductAutoCompleteBox.SelectedItem as Product;
var selectedProduct = ProductsRepository.GetProductById(_selectedProduct.ProductId);
OrderDetail orderDetail = new OrderDetail();
orderDetail.Price = selectedProduct.Price;
orderDetail.ProductCode = selectedProduct.Code;
orderDetail.ProductName = selectedProduct.Name;
orderDetail.Quantity = int.Parse(QuantityNumericUpDown.Value.ToString());
orderDetail.Um = selectedProduct.Um;
orderDetail.Total = selectedProduct.Price * int.Parse(QuantityNumericUpDown.Value.ToString());
orderDetail.Group = selectedProduct.Subgroup.Group.Name;
orderDetail.Subgroup = selectedProduct.Subgroup.Name;
orderDetail.SupplierName = selectedProduct.Supplier.Name;
//orderDetail.Order=SelectedOrder;
//orderDetail.OrderId = SelectedOrder.OrderId;
SelectedOrder.OrderDetails.Add(orderDetail);
ProductAutoCompleteBox.Text = string.Empty;
QuantityNumericUpDown.Value = 1;
ProductAutoCompleteBox.Focus();
}
and then I call the update method from repository:
public static void UpdateOrder(Order order)
{
using (RSDContext context = new RSDContext())
{
context.Orders.Attach(order);
context.Entry(order).State = EntityState.Modified;
context.SaveChanges();
}
}
I get an error about OrderId. If i set manualy the navigation property and the id I don't get an error but changes dont get saved into db.
My Order model look like this:
public class Order : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public Order()
{
_OrderDetails = new ObservableCollection<OrderDetail>();
_OrderDetails.CollectionChanged += _OrderDetails_CollectionChanged;
}
void _OrderDetails_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
AttachProductChangedEventHandler(e.NewItems.Cast<OrderDetail>());
if (e.OldItems != null)
CalcualteTotals();
}
[NotMapped]
public decimal CalculatedTotal
{
get
{
return OrderDetails.Sum(x => x.Total);
}
}
public int OrderId { get; set; }
private int _Number;
public int Number
{
get { return _Number; }
set
{
_Number = value;
NotifyPropertyChanged("Number");
}
}
private DateTime _Date;
public DateTime Date
{
get { return _Date; }
set
{
_Date = value;
NotifyPropertyChanged("Date");
}
}
private bool _Canceled;
public bool Canceled
{
get { return _Canceled; }
set
{
_Canceled = value;
NotifyPropertyChanged("Canceled");
}
}
private string _ClientName;
public string ClientName
{
get { return _ClientName; }
set
{
_ClientName = value;
NotifyPropertyChanged("ClientName");
}
}
private string _ClientPhone;
public string ClientPhone
{
get { return _ClientPhone; }
set
{
_ClientPhone = value;
NotifyPropertyChanged("ClientPhone");
}
}
private string _DeliveryAddress;
public string DeliveryAddress
{
get { return _DeliveryAddress; }
set
{
_DeliveryAddress = value;
NotifyPropertyChanged("DeliveryAddress");
}
}
private decimal _Transport;
public decimal Transport
{
get { return _Transport; }
set
{
_Transport = value;
NotifyPropertyChanged("Transport");
}
}
private decimal _Total;
public decimal Total
{
get { return _Total; }
set
{
_Total = value;
NotifyPropertyChanged("Total");
}
}
private ObservableCollection<OrderDetail> _OrderDetails;
public virtual ObservableCollection<OrderDetail> OrderDetails
{
//get { return _OrderDetails ?? (_OrderDetails = new ObservableCollection<OrderDetail>()); }
get
{
return _OrderDetails;
}
set
{
_OrderDetails = value;
NotifyPropertyChanged("OrderDetails");
}
}
private void AttachProductChangedEventHandler(IEnumerable<OrderDetail> orderDetails)
{
foreach (var p in orderDetails)
{
p.PropertyChanged += (sender, e) =>
{
switch (e.PropertyName)
{
case "Quantity":
case "Price":
case "Total":
CalcualteTotals();
break;
}
};
}
CalcualteTotals();
}
public void CalcualteTotals()
{
NotifyPropertyChanged("CalculatedTotal");
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
And my OrderDetail model look like this:
public class OrderDetail : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public int OrderDetailId { get; set; }
public int OrderId { get; set; }
public Order Order { get; set; }
private int _ProductCode;
public int ProductCode
{
get { return _ProductCode; }
set
{
_ProductCode = value;
NotifyPropertyChanged("ProductCode");
}
}
private string _ProductName;
public string ProductName
{
get { return _ProductName; }
set
{
_ProductName = value;
NotifyPropertyChanged("ProductName");
}
}
private string _Um;
public string Um
{
get { return _Um; }
set
{
_Um = value;
NotifyPropertyChanged("Um");
}
}
private decimal _Price;
public decimal Price
{
get { return _Price; }
set
{
_Price = value;
NotifyPropertyChanged("Price");
NotifyPropertyChanged("Total");
}
}
private int _Quantity;
public int Quantity
{
get { return _Quantity; }
set
{
_Quantity = value;
NotifyPropertyChanged("Quantity");
NotifyPropertyChanged("Total");
}
}
private string _SupplierName;
public string SupplierName
{
get { return _SupplierName; }
set
{
_SupplierName = value;
NotifyPropertyChanged("SupplierName");
}
}
private string _Subgroup;
public string Subgroup
{
get { return _Subgroup; }
set
{
_Subgroup = value;
NotifyPropertyChanged("Subgroup");
}
}
private string _Group;
public string Group
{
get { return _Group; }
set
{
_Group = value;
NotifyPropertyChanged("Group");
}
}
public decimal _Total;
public decimal Total
{
get { return Quantity * Price; }
set
{
_Total = value;
NotifyPropertyChanged("Total");
}
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(propertyName));
}
}
}
I'm really trying to use some sort of unit of work and I don't understand how i'm supposed to apply CRUD on objects with child collections and keep the UI updated in the same time (by working in a ObservableCollection and using Binding ClientPhone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged my parent window is updated as I type)
A final working solution:
using (RSDContext context = new RSDContext())
{
var details = order.OrderDetails;
order.OrderDetails = null;
List<int> OriginalOrderDetailsIds =
context.OrderDetails.Where(o => o.OrderId == order.OrderId).Select(o => o.OrderDetailId).ToList();
List<int> CurrentOrderDetailsIds = details.Select(o => o.OrderDetailId).ToList();
List<int> DeletedOrderDetailsIds = OriginalOrderDetailsIds.Except(CurrentOrderDetailsIds).ToList();
context.Entry(order).State = EntityState.Modified;
foreach (var deletedOrderDetailId in DeletedOrderDetailsIds)
{
context.Entry(context.OrderDetails.Single(o => o.OrderDetailId == deletedOrderDetailId)).State = EntityState.Deleted;
}
foreach (OrderDetail detail in details)
{
// Add.
if (detail.OrderDetailId == 0)
{
detail.OrderId = order.OrderId;
context.Entry(detail).State = EntityState.Added;
}
// Update.
else
{
context.Entry(detail).State = EntityState.Modified;
}
}
context.SaveChanges();
}
You could do this way for adding and updating the child, but not sure about deleted order details in the ui. If you don't want to get the order from entity, you need some kind of marking in the OrderDetail for deleted OrderDetail.
using (RSDContext context = new RSDContext())
{
var details = order.OrderDetails;
order.OrderDetails = null;
context.Entry(order).State = EntityState.Modified;
foreach (var detail in details)
{
if (detail.Id == 0)
{
// Adds.
detail.OrderId = order.Id;
context.Entry(detail).State = EntityState.Added;
}
else if (detail.IsDeleted)
// Adds new property called 'IsDeleted'
// and add [NotMapped] attribute
// then mark this property as true from the UI for deleted items.
{
// Deletes.
context.Entry(detail).State = EntityState.Deleted;
}
else
{
// Updates.
context.Entry(detail).State = EntityState.Modified;
}
}
order.OrderDetails = details;
context.SaveChanges();
}