Display new form based on GridView data - c#

I have a GridView, radGvA133s, on my main form, MainForm. I would like to be able to double-click on a row of the GridView and have that open up a new form, A133Form, to allow editing of the selected row.
Here is the double-click code:
private void radGvA133s_DoubleClick(object sender, EventArgs e)
{
A133 oA133 = (A133)A133BindingSource.CurrencyManager.List[A133BindingSource.CurrencyManager.Position];
A133Form oA133Form = new A133Form();
oA133Form.NewA133 = oA133;
oA133Form.IsNew = false;
oA133Form.ShowDialog(this);
//On return - if not cancelled, then continue
if (oA133Form.Cancelled != true)
{
this.radGvA133s.Refresh();
}
oA133Form.Dispose();
oA133Form = null;
}
Here is the A133Form code:
public partial class A133Form : Form
{
public A133Form()
{
InitializeComponent();
}
private bool _IsNew;
public bool IsNew
{
get
{
return _IsNew;
}
set
{
_IsNew = value;
}
}
private bool _Cancelled;
public bool Cancelled
{
get
{
return _Cancelled;
}
set
{
_Cancelled = value;
}
}
private A133 _newA133 = new A133();
public A133 NewA133
{
get
{
return _newA133;
}
set
{
_newA133 = value;
}
}
private void A133Form_Load(object sender, EventArgs e)
{
A133DB A133DB = new A133DB();
DataTable dtRSNs = A133DB.GetRSNList();
DataRow drFirstItem = dtRSNs.NewRow();
radComboRSN.DataSource = dtRSNs;
drFirstItem["rsn_id"] = "0";
drFirstItem["rsn_name"] = "";
dtRSNs.Rows.InsertAt(drFirstItem, 0);
radComboRSN.ValueMember = "rsn_id";
radComboRSN.DisplayMember = "rsn_name";
//Set databindings
radComboRSN.DataBindings.Add(new Binding("Text", NewA133, "RSN", true, DataSourceUpdateMode.OnPropertyChanged));
radTxtSubcontractor.DataBindings.Add(new Binding("Text", NewA133, "Subcontractor", true, DataSourceUpdateMode.OnPropertyChanged));
radMTxtCFDANumber.DataBindings.Add(new Binding("Text", NewA133, "CFDANumber", true, DataSourceUpdateMode.OnPropertyChanged));
radCbIncludeCFDA.DataBindings.Add(new Binding("Checked", NewA133, "IncludeCFDA", true, DataSourceUpdateMode.OnPropertyChanged));
radMTxtYear.DataBindings.Add(new Binding("Text", NewA133, "sYear", true, DataSourceUpdateMode.OnPropertyChanged));
radTxtFedAward.DataBindings.Add(new Binding("Text", NewA133, "FedAward", true, DataSourceUpdateMode.OnPropertyChanged));
radCbExceeds.DataBindings.Add(new Binding("Checked", NewA133, "Exceeds", true, DataSourceUpdateMode.OnPropertyChanged));
radDTPDateMHDReceived.DataBindings.Add(new Binding("Value", NewA133, "DateMHDReceived", true, DataSourceUpdateMode.OnPropertyChanged));
radDTPPeriodEnding.DataBindings.Add(new Binding("Value", NewA133, "PeriodEnding", true, DataSourceUpdateMode.OnPropertyChanged));
radDTPDateAudited.DataBindings.Add(new Binding("Value", NewA133, "DateAudited", true, DataSourceUpdateMode.OnPropertyChanged));
radDTPForwardDate.DataBindings.Add(new Binding("Value", NewA133, "ForwardDate", true, DataSourceUpdateMode.OnPropertyChanged));
radTxtSAOPerson.DataBindings.Add(new Binding("Text", NewA133, "SAOPerson", true, DataSourceUpdateMode.OnPropertyChanged));
}
private void radBtnCancel_Click(object sender, EventArgs e)
{
this.Cancelled = true;
this.Close();
}
private void radBtnSave_Click(object sender, EventArgs e)
{
this.Cancelled = false;
bool bValid = true;
foreach(Control control in this.Controls)
{
if (Convert.ToString(control.Tag) == "Required")
{
bool bMissingInfo = false;
if (control is RadDateTimePicker)
{
RadDateTimePicker dtp = control as RadDateTimePicker;
if (dtp.Value.ToString() == "1/1/0001 12:00:00 AM")
{
bMissingInfo = true;
}
}
else
{
if (string.IsNullOrEmpty(control.Text))
{
bMissingInfo = true;
}
}
if (bMissingInfo == true)
{
errorProvider1.SetError(control, "* Required Field");
bValid = false;
}
else
{
errorProvider1.SetError(control, "");
}
}
}
if (bValid == true)
{
bool bSaved = NewA133.SaveData();
if (bSaved == true)
{
this.Close();
}
else
{
MessageBox.Show("There was an error saving the data! If this continues, please contact technical assistance.",
"Error Saving Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("The information you have entered is incomplete. Please fill out all required fields.",
"Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
And, finally, here is the A133 Class code:
#region A133 Collection
public class A133Collection : BindingListView<A133>
{
public A133Collection() : base()
{
}
public A133Collection(List<A133> a133s) : base(a133s)
{
}
public A133Collection(DataTable dt)
{
foreach(DataRow oRow in dt.Rows)
{
A133 a = new A133(oRow);
this.Add(a);
}
}
private int FindMaxId()
{
int maxId = -1;
foreach(A133 a in this)
{
if (a.A133Id > maxId)
{
maxId = a.A133Id;
}
}
return maxId;
}
}
#endregion
#region A133 Class
///<summary>
///Class: A133
///Desc: Manages a single A133 business object
/// Note - 4 states for the object: Unchanged, Added, Deleted, Modified
/// Added flags that a brand new object was added; set prior to db insert
/// Deleted flags that the object was deleted; set after db delete
/// Unchanged is default state
/// Modified flags that props have changed
/// >> The IsDirty indicator looks to see if the object is "modified" or "added"
/// since these are pre-database flags
///</summary>
public class A133 : INotifyPropertyChanged, IEditableObject, IDataErrorInfo
{
//Declare internal class collection object
private A133Collection _A133s = new A133Collection();
//Declare internal class objects
private MHDFMS.BusinessLogic.A133DB _DB = new A133DB();
//Declare internal class props
private int _A133Id;
private string _RSN;
private string _Subcontractor;
private string _CFDANumber;
private string _IncludeCFDA;
private string _Year;
private string _FedAward;
private string _Exceeds;
private string _DateMHDReceived;
private string _PeriodEnding;
private string _DateAudited;
private string _ForwardDate;
private string _SAOPerson;
private int _OldA133Id;
private string _OldRSN;
private string _OldSubcontractor;
private string _OldCFDANumber;
private string _OldIncludeCFDA;
private string _OldYear;
private string _OldFedAward;
private string _OldExceeds;
private string _OldDateMHDReceived;
private string _OldPeriodEnding;
private string _OldDateAudited;
private string _OldForwardDate;
private string _OldSAOPerson;
private bool _Editing;
private string _Error = string.Empty;
private EntityStateEnum _EntityState;
private Hashtable _PropErrors = new Hashtable();
private void FirePropertyChangeNotification(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public A133()
{
this.EntityState = EntityStateEnum.Unchanged;
}
public A133(DataRow dr)
{
//Populates the business object item from a data row
this.A133Id = Convert.ToInt32(dr["a133_id"]);
this.RSN = dr["rsn"].ToString();
this.Subcontractor = dr["subcontractor"].ToString();
this.CFDANumber = dr["cfda_no"].ToString();
this.IncludeCFDA = dr["include_cfda"].ToString();
this.sYear = dr["year"].ToString();
this.FedAward = dr["fed_award"].ToString();
this.Exceeds = dr["exceeds"].ToString();
if (dr["date_mhd_received"] != null)
{
this.DateMHDReceived = Convert.ToDateTime(dr["date_mhd_received"]).ToShortDateString();
}
if (dr["period_ending"] != null)
{
this.PeriodEnding = Convert.ToDateTime(dr["period_ending"]).ToShortDateString();
}
if (dr["date_audited"] != null)
{
this.DateAudited = Convert.ToDateTime(dr["date_audited"]).ToShortDateString();
}
if (dr["forward_date"] != null)
{
this.ForwardDate = Convert.ToDateTime(dr["forward_date"]).ToShortDateString();
}
this.SAOPerson = dr["sao_person"].ToString();
this.EntityState = EntityStateEnum.Unchanged;
}
#region Public Methods/Constructors
public bool SaveData()
{
bool bSaved = false;
if (this.A133Id == 0)
{
bSaved = _DB.SaveA133Data(this);
}
else
{
bSaved = _DB.SaveA133Data(this);
}
if (bSaved == true)
{
this.EntityState = EntityStateEnum.Unchanged;
}
return bSaved;
}
public bool Delete()
{
bool bSaved = _DB.SaveA133AsInactive(this);
if (bSaved == true)
{
this.EntityState = EntityStateEnum.Deleted;
}
return bSaved;
}
public Int32 A133Id
{
get
{
return _A133Id;
}
set
{
_A133Id = value;
}
}
public string RSN
{
get
{
return _RSN;
}
set
{
_RSN = value;
FirePropertyChangeNotification("RSN");
}
}
public string Subcontractor
{
get
{
return _Subcontractor;
}
set
{
_Subcontractor = value;
FirePropertyChangeNotification("Subcontractor");
}
}
public string CFDANumber
{
get
{
return _CFDANumber;
}
set
{
_CFDANumber = value;
FirePropertyChangeNotification("CFDANumber");
}
}
public string IncludeCFDA
{
get
{
return _IncludeCFDA;
}
set
{
_IncludeCFDA = value;
FirePropertyChangeNotification("IncludeCFDA");
}
}
public string sYear
{
get
{
return _Year;
}
set
{
_Year = value;
FirePropertyChangeNotification("sYear");
}
}
public string FedAward
{
get
{
return _FedAward;
}
set
{
_FedAward = value;
FirePropertyChangeNotification("FedAward");
}
}
public string Exceeds
{
get
{
return _Exceeds;
}
set
{
_Exceeds = value;
FirePropertyChangeNotification("Exceeds");
}
}
public string DateMHDReceived
{
get
{
return _DateMHDReceived;
}
set
{
_DateMHDReceived = value;
FirePropertyChangeNotification("DateMHDReceived");
}
}
public string PeriodEnding
{
get
{
return _PeriodEnding;
}
set
{
_PeriodEnding = value;
FirePropertyChangeNotification("PeriodEnding");
}
}
public string DateAudited
{
get
{
return _DateAudited;
}
set
{
_DateAudited = value;
FirePropertyChangeNotification("DateAudited");
}
}
public string ForwardDate
{
get
{
return _ForwardDate;
}
set
{
_ForwardDate = value;
FirePropertyChangeNotification("ForwardDate");
}
}
public string SAOPerson
{
get
{
return _SAOPerson;
}
set
{
_SAOPerson = value;
FirePropertyChangeNotification("SAOPerson");
}
}
public Boolean IsDirty
{
get
{
return ((this.EntityState != EntityStateEnum.Unchanged) || (this.EntityState != EntityStateEnum.Deleted));
}
}
public enum EntityStateEnum
{
Unchanged,
Added,
Deleted,
Modified
}
public A133Collection A133s
{
get
{
return _A133s;
}
}
void IEditableObject.BeginEdit()
{
if (!_Editing)
{
_OldA133Id = _A133Id;
_OldRSN = _RSN;
_OldSubcontractor = _Subcontractor;
_OldCFDANumber = _CFDANumber;
_OldIncludeCFDA = _IncludeCFDA;
_OldYear = _Year;
_OldFedAward = _FedAward;
_OldExceeds = _Exceeds;
_OldDateMHDReceived = _DateMHDReceived;
_OldPeriodEnding = _PeriodEnding;
_OldDateAudited = _DateAudited;
_OldForwardDate = _ForwardDate;
_OldSAOPerson = _SAOPerson;
}
this.EntityState = EntityStateEnum.Modified;
_Editing = true;
}
void IEditableObject.CancelEdit()
{
if (_Editing)
{
_A133Id = _OldA133Id;
_RSN = _OldRSN;
_Subcontractor = _OldSubcontractor;
_CFDANumber = _OldCFDANumber;
_IncludeCFDA = _OldIncludeCFDA;
_Year = _OldYear;
_FedAward = _OldFedAward;
_Exceeds = _OldExceeds;
_DateMHDReceived = _OldDateMHDReceived;
_PeriodEnding = _OldPeriodEnding;
_DateAudited = _OldDateAudited;
_ForwardDate = _OldForwardDate;
_SAOPerson = _OldSAOPerson;
}
this.EntityState = EntityStateEnum.Unchanged;
_Editing = false;
}
void IEditableObject.EndEdit()
{
_Editing = false;
}
public EntityStateEnum EntityState
{
get
{
return _EntityState;
}
set
{
_EntityState = value;
}
}
string IDataErrorInfo.Error
{
get
{
return _Error;
}
}
string IDataErrorInfo.this[string columnName]
{
get
{
return (string)_PropErrors[columnName];
}
}
private void DataStateChanged(EntityStateEnum dataState, string propertyName)
{
//Raise the event
if (PropertyChanged != null && propertyName != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
//If the state is deleted, mark it as deleted
if (dataState == EntityStateEnum.Deleted)
{
this.EntityState = dataState;
}
if (this.EntityState == EntityStateEnum.Unchanged)
{
this.EntityState = dataState;
}
}
#endregion
}
#endregion
Unfortunately, when I double-click on the GridView, I receive this error: "InvalidCastException was unhandled. Unable to cast object of type 'System.Data.DataRowView' to type 'MHDFMS.BusinessLogic.A133'"
This error occurs at the very first line of the double-click event.
I am at a loss here and have been pulling my hair out for some time. Am I missing something obvious? Is there an easier (or better!) way to achieve my desired result?
Any help is greatly appreciated!

Try this:
DataRowView currentRow = A133BindingSource.CurrencyManager.List[A133BindingSource.CurrencyManager.Position] as DataRowView;
A133Form oA133Form = new A133Form();
oA133Form.NewA133 = new A133(currentRow.Row);

You forgot to post the event handler causing the exception... ;)
But the source of the problem seem quite obvious - you get a DataRowView object from the property grid and try to cast it (maybe by assigning to a variable) to A133 were you probably wanted new A133(DataRow dataRow).

Related

c#: PropertyChanged always null

Like the title says, I try to notify a property change, the method RaisePropertyChanged is called coorectly, but PropertyChanged is always null.
Here the shortened class:
public class BluetoothManager : INotifyPropertyChanged {
private string selectedBluetoothResult;
private List<string> foundDevices = new List<string>(5);
public List<DeviceInformation> penDevices = new List<DeviceInformation>();
private GattCharacteristic TxCharacteristic;
public string SelectedBluetoothResult {
get {
return selectedBluetoothResult;
}
set {
selectedBluetoothResult = value;
RaisePropertyChanged();
}
}
public List<string> FoundDevices {
get
{
return foundDevices;
}
set
{
foundDevices = value;
RaisePropertyChanged();
}
}
public BluetoothManager() {
StartScanWatcher();
}
public void StartScanWatcher() {
Debug.WriteLine("Starting device watcher...");
String query = "";
//query for Bluetooth LE devices
query += "System.Devices.DevObjectType:=5 AND System.Devices.Aep.ProtocolId:=\"{BB7BB05E-5972-42B5-94FC-76EAA7084D49}\"";
//query for devices with controllers' name
query += " AND (System.ItemNameDisplay:=\"" + DeviceName + "\" )";
var deviceWatcher = DeviceInformation.CreateWatcher(query); //, requestedProperties, DeviceInformationKind.AssociationEndpoint);
deviceWatcher.Added += DeviceWatcher_OnAdded;
deviceWatcher.EnumerationCompleted += DeviceWatcher_OnEnumComplete;
deviceWatcher.Removed += DeviceWatcher_Removed;
deviceWatcher.Stopped += DeviceWatcher_Stopped;
deviceWatcher.Updated += DeviceWatcher_Updated;
deviceWatcher.Start();
Debug.WriteLine(" StartScanWatcher end");
}
private void DeviceWatcher_OnAdded(DeviceWatcher sender, DeviceInformation deviceInfo) {
Debug.WriteLine(" DeviceWatcher_OnAdded Start");
lock (foundDevices) {
if (foundDevices.Contains(deviceInfo.Id)) {
return;
}
foundDevices.Add(deviceInfo.Id);
RaisePropertyChanged("FoundDevices");
}
Debug.WriteLine($"[{deviceInfo.Name}] DeviceWatcher_OnAdded...");
if (SelectedBluetoothResult == null)
{
SelectedBluetoothResult = deviceInfo.Id;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
Debug.WriteLine("<<<<<<<<<<<<< BluetoothManager, PropertyChanging: " + propertyName);
if (PropertyChanged == null) {
Debug.WriteLine("============ BluetoothManager, PropertyChanged == null, " + propertyName);
return;
}
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
Debug.WriteLine(">>>>>>>>>>>>> BluetoothManager, PropertyChanged: " + propertyName);
}
}
}
And here the Binding in the XAML file:
<ListView ItemsSource="{Binding BluetoothManager.FoundDevices}" SelectedItem="{Binding BluetoothManager.SelectedBluetoothResult}">
<ListView.Resources>
<DataTemplate x:Key="BluetoothDeviceTemplate">
<TextBlock Text="{Binding Path=Sap}"/>
</DataTemplate>
</ListView.Resources>
</ListView>
The binding seems to work properly.
Please note, that the class in which the PropertyChanged is always null is not the DataContext auf the XAML file. Does that mean I have to work differently with the PropertyChange notification?
Thanks in advance.
EDIT:
The complete MainWindowViewModel:
public class MainWindowViewModel : INotifyPropertyChanged {
private ObservableCollection<BookGame> _booksToDisplay = new ObservableCollection<BookGame>();
private ObservableCollection<BookGame> _games = new ObservableCollection<BookGame>();
private string _url;
private int[] _newBooksMID;
private int[] _oldBooksMID;
private Dictionary<int, int> _newBooksVersionNumbers;
private Dictionary<int, int> _oldBooksVersionNumbers;
private Dictionary<int, int> _allVersionNumbers;
private List<BookGame> _booksToAdd;
private long _downloadSpeed;
private bool _isDownloading = true;
private bool _toggleLastSearchKeyWasReturn = false;
string _volumeLabel = "";
FileInfo[] _filesTxt = { };
FileInfo[] _filesAll = { };
private string _folderPath = "";
private string _driveName = null;
List<BookGame> _allGames;
List<BookGame> _allBooks;
List<MP3> _allMP3;
long lengthAllBooks = 0;
long lengthAllGames = 0;
int _percentDownloaded = 100;
private long _amountBytesToDownload;
private long _amountBytesDownloaded;
private long _amountMBytesToDownload;
private long _amountMBytesDownloaded;
private int _downloadTime;
//private bool _isDownloadAborted;
ServerCommi serverComm;
public BluetoothManager BluetoothManager { get; set; }
public MainWindowViewModel() {
DownloadTime = 0;
AmountBytesToDownload = 0;
AmountBytesDownloaded = 0;
DriveInfo drive = null;
foreach (DriveInfo driveInfo in DriveInfo.GetDrives()) {
if (driveInfo.IsReady && driveInfo.VolumeLabel == _volumeLabel) {
drive = driveInfo;
_driveName = drive.Name;
_folderPath = _driveName + _folderPath;
}
}
DirectoryInfo di = new DirectoryInfo(_folderPath);
if (di.Exists)
{
_filesTxt = di.GetFiles("*.txt");
FilesAll = di.GetFiles("*.*");
foreach (FileInfo file in _filesTxt)
{
try
{
Convert.ToInt32(file.Name.Split('_')[0]);
AddBookGameToList(file);
}
catch (Exception e)
{
}
}
}
SearchResults = new ObservableCollection<ResultItem>();
MenuCommand = new RelayCommand(o => {
Debug.WriteLine("Menu Command " + o);
SwitchBooks(o);
});
SearchReturnKeyCommand = new RelayCommand(o => {
Debug.WriteLine("00000000000000000000000000000000 SearchReturnKeyCommand " + o);
SearchActivated();
});
BrowserCommand = new RelayCommand(o => {
Debug.WriteLine("Browser Command main" + o);
CallBrowser("");
});
DeleteCommand = new RelayCommand(o => {
Debug.WriteLine("Delete Command main" + o);
});
ToggleDownloadsCommand = new RelayCommand(o => {
Debug.WriteLine(" |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ");
Debug.WriteLine(" |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ");
Debug.WriteLine("ToggleDownloadsCommand Command main" + o);
ToggleDownloads();
});
_viewModelMusic = new ViewModelMusic(_driveName);
AllGames = Games.ToList<BookGame>();
AllBooks = BooksToDisplay.ToList<BookGame>();
AllMP3 = _viewModelMusic.Mp3s.ToList<MP3>();
long lengthAllMP3 = 0;
foreach (MP3 mp3 in AllMP3) {
lengthAllMP3 += mp3.LengthValue;
}
_viewModelBooks = new ViewModelBooks(BooksToDisplay);
_viewModelGames = new ViewModelGames(Games);
_viewModelFiles = new ViewModelFiles(FilesAll);
_viewModelLumi = new ViewModelLumi(drive, lengthAllBooks, lengthAllGames, lengthAllMP3);
_viewModelOverview = new ViewModelOverview(AllBooks, AllGames, AllMP3);
_screens[0] = _viewModelOverview;
_screens[1] = _viewModelBooks;
_screens[2] = _viewModelGames;
_screens[3] = _viewModelMusic;
_screens[4] = _viewModelFiles;
_screens[5] = _viewModelVideos;
_screens[6] = _viewModelAdults;
_screens[7] = _viewModelLumi;
SearchText = "";
SelectedItem = _viewModelBooks;
Debug.WriteLine("CALLING ServerCommi! Ring, ring!");
serverComm = new ServerCommi(this);
//serverComm.DownloadBooksAsync();
BluetoothManager = new BluetoothManager();
}
private void ToggleDownloads() {
IsDownloading = !IsDownloading;
serverComm.ToggleDownloads(IsDownloading);
_viewModelBooks.ToggleDownloads(IsDownloading);
}
internal void DownloadStateChange(int mid, int newState) {
_viewModelBooks.DownloadStateChange(mid, newState);
}
// params bool[] isDownload : varargs
// returns the mid
public int AddBookGameToList(FileInfo file, bool isDownload = false) {
BookGame bg = new BookGame(file);
if (isDownload) {
bg.DownloadState = 2;
if (bg.Mid == serverComm.DownloadingMID) {
bg.DownloadState = 1;
}
}
if (bg.Group.StartsWith("B")) {
bg.Group = "Bücher";
}
switch (bg.Group) {
case "Bücher":
if (isDownload) {
BooksToDisplay.Insert(0, bg);
} else {
BooksToDisplay.Add(bg);
}
lengthAllBooks += bg.LengthValue;
break;
case "Spiele":
Games.Add(bg);
lengthAllGames += bg.LengthValue;
break;
default:
Debug.WriteLine("Default: " + bg.Title);
break;
}
return bg.Mid;
}
private void CallBrowser(string url) {
Debug.WriteLine("Url: " + Url);
try {
System.Diagnostics.Process.Start(Url);
} catch (System.ComponentModel.Win32Exception noBrowser) {
if (noBrowser.ErrorCode == -2147467259)
MessageBox.Show(noBrowser.Message);
} catch (System.Exception other) {
MessageBox.Show(other.Message);
}
}
string _searchText;
public string SearchText {
get {
return _searchText;
}
set {
Debug.WriteLine("SearchText Value= " + value);
if (!_toggleLastSearchKeyWasReturn) {
_searchText = value;
SearchResults.Clear();
List<ResultItem> _allBooksRI = new List<ResultItem>();
List<ResultItem> _allBooksHelperList = _allBooks.ToList<ResultItem>();
List<ResultItem> _allGamesHelperList = _allGames.ToList<ResultItem>();
List<ResultItem> _allMP3HelperList = _allMP3.ToList<ResultItem>();
if (SelectedItem != null && SelectedItem.Equals(_viewModelGames)) {
AddAllResultItemsIf(SearchResults, _allGamesHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allBooksHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allMP3HelperList, _searchText);
Debug.WriteLine("===================================== Games - " + SearchResults);
Debug.WriteLine("SelectedItem - " + SelectedItem);
} else if (SelectedItem != null && SelectedItem.Equals(_viewModelMusic)) {
AddAllResultItemsIf(SearchResults, _allMP3HelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allGamesHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allBooksHelperList, _searchText);
Debug.WriteLine("====================================== Music " + SearchResults);
Debug.WriteLine("SelectedItem - " + SelectedItem);
} else {
AddAllResultItemsIf(SearchResults, _allBooksHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allGamesHelperList, _searchText);
AddAllResultItemsIf(SearchResults, _allMP3HelperList, _searchText);
Debug.WriteLine("====================================== Books " + SearchResults);
}
if (SearchResults.Count == 0) {
SearchResults.Add(new ErrorResultItem("Error", "Nichts passendes gefunden."));
}
} else {
_toggleLastSearchKeyWasReturn = false;
}
}
}
private ObservableCollection<ResultItem> AddAllResultItemsIf(ObservableCollection<ResultItem> searchResults, List<ResultItem> toAdd, string searchText) {
foreach (ResultItem item in toAdd) {
if (item.Title.ToLower().Contains(_searchText.ToLower())) {
searchResults.Add(item);
}
}
return searchResults;
}
public ObservableCollection<ResultItem> SearchResults {
get; set;
}
ResultItem _selectedResult;
public ResultItem SelectedResult {
get {
return _selectedResult;
}
set {
_selectedResult = value;
SearchItemSelected(value);
}
}
private void SearchItemSelected(ResultItem value) {
switch (value.Group) {
case "Bücher":
SelectedItem = _viewModelBooks;
break;
case "Spiele":
SelectedItem = _viewModelGames;
break;
case "Musik":
SelectedItem = _viewModelMusic;
break;
default:
Debug.WriteLine("Search Item Selected, jumped to default: " + value);
break;
}
Unmark(Marked);
Mark(value);
}
ResultItem _marked;
internal void Mark(ResultItem value) {
Marked = value;
value.Marked = true;
}
internal void Unmark(ResultItem value) {
Marked = null;
if (value != null) {
value.Marked = false;
}
}
public ResultItem Marked {
get => _marked;
set => _marked = value;
}
private bool _isSearchResult;
public bool IsSearchResult {
get {
return _isSearchResult;
}
set {
_isSearchResult = value;
Debug.WriteLine("IsSearchResult= " + value);
RaisePropertyChanged();
}
}
private void SearchActivated() {
_toggleLastSearchKeyWasReturn = true;
SelectedItem = _viewModelOverview;
IsSearchResult = true;
}
private object _selectedItem;
public object SelectedItem {
get {
return _selectedItem;
}
set {
_selectedItem = value;
Debug.WriteLine("SELECTED_ITEM SETTER: " + value);
Unmark(Marked);
IsSearchResult = false;
if (SearchText != null) {
SearchText = SearchText;
}
RaisePropertyChanged();
}
}
ViewModelOverview _viewModelOverview;
ViewModelBooks _viewModelBooks;
ViewModelGames _viewModelGames;
ViewModelMusic _viewModelMusic;
ViewModelFiles _viewModelFiles;
ViewModelVideos _viewModelVideos = new ViewModelVideos();
ViewModelAdults _viewModelAdults = new ViewModelAdults();
ViewModelLumi _viewModelLumi;
object[] _screens = new object[8];
public object[] Screens {
get {
return _screens;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged([CallerMemberName] string propertyName = null) {
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public ICommand MenuCommand {
get; set;
}
public ICommand SearchReturnKeyCommand {
get; set;
}
public ICommand BrowserCommand {
get; set;
}
public ICommand ToggleDownloadsCommand {
get; set;
}
public RelayCommand DeleteCommand {
get;
private set;
}
public List<BookGame> AllGames {
get => _allGames;
set => _allGames = value;
}
public List<BookGame> AllBooks {
get => _allBooks;
set => _allBooks = value;
}
public List<MP3> AllMP3 {
get => _allMP3;
set => _allMP3 = value;
}
public ViewModelBooks ViewModelBooks {
get => _viewModelBooks;
set => _viewModelBooks = value;
}
public ObservableCollection<BookGame> BooksToDisplay {
get => _booksToDisplay;
set => _booksToDisplay = value;
}
public ObservableCollection<BookGame> Games {
get => _games;
set => _games = value;
}
public string Url {
get {
return _url;
}
set {
_url = value;
RaisePropertyChanged();
}
}
public int[] NewBooksMID {
get => _newBooksMID;
set => _newBooksMID = value;
}
public int[] OldBooksMID {
get => _oldBooksMID;
set => _oldBooksMID = value;
}
public Dictionary<int, int> NewBooksVersionNumbers {
get => _newBooksVersionNumbers;
set => _newBooksVersionNumbers = value;
}
public Dictionary<int, int> OldBooksVersionNumbers {
get => _oldBooksVersionNumbers;
set => _oldBooksVersionNumbers = value;
}
public Dictionary<int, int> AllVersionNumbers {
get => _allVersionNumbers;
set => _allVersionNumbers = value;
}
public int[] OldBooksMID1 {
get => _oldBooksMID;
set => _oldBooksMID = value;
}
public List<BookGame> BooksToAdd {
get => _booksToAdd;
set => _booksToAdd = value;
}
public FileInfo[] FilesAll {
get => _filesAll;
set => _filesAll = value;
}
public string FolderPath {
get => _folderPath;
set => _folderPath = value;
}
public int PercentDownloaded {
get {
return _percentDownloaded;
}
set {
_percentDownloaded = value;
RaisePropertyChanged();
}
}
public long DownloadSpeed {
get {
return _downloadSpeed;
}
set {
_downloadSpeed = value;
RaisePropertyChanged();
}
}
public long AmountBytesToDownload {
get {
return _amountBytesToDownload;
}
set {
_amountBytesToDownload = value;
Debug.WriteLine("Property Changed: " + "AmountBytesToDownload");
AmountMBytesToDownload = value / 1024 / 1024;
}
}
public long AmountBytesDownloaded {
get {
return _amountBytesDownloaded;
}
set {
_amountBytesDownloaded = value;
AmountMBytesDownloaded = value / 1024 / 1024;
}
}
public int DownloadTime {
get {
return _downloadTime;
}
set {
_downloadTime = value;
RaisePropertyChanged();
}
}
/*
public bool IsDownloadAborted {
get {
return _isDownloadAborted;
}
set {
_isDownloadAborted = value;
RaisePropertyChanged();
}
}
*/
public long AmountMBytesDownloaded {
get {
return _amountMBytesDownloaded;
}
set {
_amountMBytesDownloaded = value;
RaisePropertyChanged();
}
}
public long AmountMBytesToDownload {
get {
return _amountMBytesToDownload;
}
set {
_amountMBytesToDownload = value;
RaisePropertyChanged();
}
}
public bool IsDownloading {
get {
return _isDownloading;
}
set {
_isDownloading = value;
RaisePropertyChanged();
}
}
internal void SwitchBooks(object o) {
Debug.WriteLine("SwitchBooksEx " + o);
if (o.ToString().Equals("Tessloff.ViewModelBooks")) {
((ViewModelBooks)_screens[0]).SwitchView();
Debug.WriteLine("SwitchBooksIn " + o);
}
}
}
public class CommandViewModel {
private MainWindowViewModel _viewmodel;
public CommandViewModel(MainWindowViewModel viewmodel) {
_viewmodel = viewmodel;
Debug.WriteLine("LALALALALA");
MenuCommand = new RelayCommand(o => {
Debug.WriteLine("CommandViewModel " + o);
_viewmodel.SwitchBooks(o);
});
DeleteCommand = new RelayCommand(o => {
Debug.WriteLine("Delte Command CVM" + o);
});
}
public ICommand MenuCommand {
get; set;
}
public ICommand DeleteCommand {
get; set;
}
public string Title {
get;
private set;
}
}
public class RelayCommand : ICommand {
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute)
: this(execute, null) {
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute) {
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter) {
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged {
add {
CommandManager.RequerySuggested += value;
}
remove {
CommandManager.RequerySuggested -= value;
}
}
public void Execute(object parameter) {
_execute(parameter);
}
#endregion // ICommand Members
}
public class ViewModelAdults {
public ViewModelAdults() {
Title = "Erwachsene";
ImgUrl = "/Resources/Erwachsene.png";
}
public string Title {
get; set;
}
public string ImgUrl {
get;
private set;
}
}
Edit tl;dr:
Why do all "direct" properties of MainWindowViewModel update great (like MainWindowViewModel.IsSearchResult), but the two "indirect" properties don't (MainWindowViewModel.BluetoothManager.SelectedBluetoothResul‌​t).
List.add() doesnt trigger PropertyChange. You need to use ObservableCollection or raise PropertyChange yourself after adding item.

mvvm selected listview items to list<string> in object

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();
}

Why my ICollection is always empty?

I am trying to reach a foreach but my program never gets inside because my ICollection Coletores is always empty even if I put a lot of stuff in there.
The code where I want to get inside and it is always empty (I want to get inside the second foreach):
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (NavigationContext.QueryString.TryGetValue("email", out email))
{
//MessageBox.Show(email);
List<HyperlinkButton> listaLinks = new List<HyperlinkButton>();
AppDataContext db = new AppDataContext();
int i = 0;
HyperlinkButton aux = new HyperlinkButton();
foreach (Pessoa pessoa in db.Pessoas)
{
if (pessoa.Email == email)
{
foreach (Coletor coletor in pessoa.Coletores)
{
aux.Content = "Coletor " + (i + 1).ToString();
aux.FontSize = 24;
aux.NavigateUri = new Uri("/OcorrenciasPage.xaml?coletorId=" + coletor.Id.ToString(), UriKind.RelativeOrAbsolute);
listaLinks.Add(aux);
i++;
}
}
}
ListBox coletores = new ListBox();
coletores.ItemsSource = listaLinks;
stcList.Children.Add(coletores);
}
base.OnNavigatedTo(e);
}
Now the code that I am adding data:
if (txtLat.Text != "" && txtLong.Text != "" && rdNorte.IsChecked == true || rdSul.IsChecked == true && rdLeste.IsChecked == true || rdOeste.IsChecked == true)
{
foreach (var pessoa in db.Pessoas)
{
if (pessoa.Email == email)
{
pessoa.Coletores.Add(coletor);
}
}
db.Coletores.InsertOnSubmit(coletor);
db.SubmitChanges();
NavigationService.Navigate(new Uri("/ColetoresPage.xaml?email=" + email, UriKind.RelativeOrAbsolute));
}
Now, my Pessoa class:
public class Pessoa : INotifyPropertyChanged
{
private int _id;
[Column(IsDbGenerated = true, IsPrimaryKey = true)]
public int Id {
get { return _id;}
set { _id = value;
OnPropertyChanged("Id");
}
}
private string _nome;
[Column]
public string Nome
{
get { return _nome; }
set { _nome = value;
OnPropertyChanged("Nome");
}
}
private string _email;
[Column]
public string Email
{
get { return _email; }
set { _email = value;
OnPropertyChanged("Email");
}
}
private string _senha;
[Column]
public string Senha { get { return _senha; }
set { _senha = value;
OnPropertyChanged("Senha");
}
}
private string _profissao;
[Column]
public string Profissao { get { return _profissao; }
set { _profissao = value;
OnPropertyChanged("Profissao");
}
}
private int _idade;
[Column]
public int Idade { get { return _idade; }
set { _idade = value;
OnPropertyChanged("Idade");
}
}
private string _endereco;
[Column]
public string Endereco { get { return _endereco; }
set { _endereco = value;
OnPropertyChanged("Endereco");
}
}
private string _cidade;
[Column]
public string Cidade { get { return _cidade; }
set { _cidade = value;
OnPropertyChanged("Cidade");
}
}
private string _estado;
[Column]
public string Estado { get { return _estado; }
set { _estado = value;
OnPropertyChanged("Estado");
}
}
private EntitySet<Coletor> _coletores = new EntitySet<Coletor>();
[Association(Name = "FK_Coletores_PessoaColetores", Storage = "_coletores", ThisKey = "Id", OtherKey = "pessoaId")]
public ICollection<Coletor> Coletores
{
get { return _coletores; }
set { _coletores.Assign(value); }
}
private EntitySet<PessoaColetor> _pessoaColetores = new EntitySet<PessoaColetor>();
[Association(Name = "FK_PessoaColetores_Pessoas", Storage = "_pessoaColetores", OtherKey = "pessoaId", ThisKey = "Id")]
private ICollection<PessoaColetor> PessoaColetores
{
get { return _pessoaColetores; }
set { _pessoaColetores.Assign(value); }
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
The Coletores property on the Pessoa class looks alright. There is also a PessoaColetores property. Are you sure there has been no confusion between the two? Especially with intellisense one might dot or tab the wrong one.
Have you put a break point on the line that actually adds coletors (second code snippet)? Maybe stuff is added to the wrong collection.
Cheers, B.

Entity Framework 6.1: CRUD on Child objects

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();
}

SerializationException when serializing instance of a class which implements INotifyPropertyChanged

i am trying to serialize a field of my class. Withou it serialization is fine, with it a get SerializationException.
Field is : private readonly ObservableCollection<CellVM> Values;
Exception is
Type System.ComponentModel.PropertyChangedEventManager in assembly WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 is not marked as serializable.
I am targeting Framework 3.5.
I found some suggestions, that it could be problem with serialization of observalble collections, but that should be fixed by 3.5 sp1.
Have no idea how to fix that, any ideas? thank you.
CellVM class:
[Serializable]
public class CellVM:ANotifyPropertyChanged
{
public Cell model;
public int X
{
get { return model.X; }
set { model.X = value; OnPropertyChanged("X"); }
}
public int Y
{
get { return model.Y; }
set { model.Y = value; OnPropertyChanged("Y"); }
}
public string Value
{
get
{
return model.ActualValue;
}
set { model.ActualValue = value; OnPropertyChanged("Value"); }
}
[NonSerialized]
private bool _isActive;
public bool IsActive
{
get
{
return _isActive;
}
set
{
_isActive = value;
OnPropertyChanged("IsActive");
OnPropertyChanged("BackgroundBrush");
OnPropertyChanged("HighlightBrush");
}
}
public bool IsReadOnly
{
get
{
if(model.InitialValue.Equals(model.RightValue))
{
return true;
}
return false;
}
}
public bool IsHighLighted
{
get;
set;
}
private bool _isInvalid;
public bool IsInvalid
{
get { return _isInvalid; }
set
{
_isInvalid = value;
OnPropertyChanged("IsInvalid");
OnPropertyChanged("BackgroundBrush");
}
}
private bool _isValueMode;
public bool IsValueMode
{
get
{
return _isValueMode;
}
set
{
_isValueMode = value;
OnPropertyChanged("IsValueMode");
OnPropertyChanged("ValueVisibility");
OnPropertyChanged("PossibilityVisibility");
}
}
[NonSerialized]
private FontWeight _valueFontWeight;
public FontWeight ValueFontWeight
{
get
{
return _valueFontWeight;
}
set { _valueFontWeight = value; OnPropertyChanged("ValueFontWeight");}
}
[NonSerialized]
private Brush _valueColor;
public Brush ValueColor
{
get
{
if(_valueColor == null)
{
return new SolidColorBrush(Colors.Black);
}
return _valueColor;
}
set { _valueColor = value; OnPropertyChanged("ValueColor"); }
}
public Visibility ValueVisibility
{
get
{
if(IsValueMode)
{
return Visibility.Visible;
}
return Visibility.Hidden;
}
}
public Visibility PossibilityVisibility
{
get
{
if (!IsValueMode)
{
return Visibility.Visible;
}
return Visibility.Hidden;
}
}
private bool _isCheckInvalid;
public bool IsCheckInvalid
{
get
{
return _isCheckInvalid;
}
set
{
_isCheckInvalid = value;
OnPropertyChanged("IsCheckInvalid");
OnPropertyChanged("HighlightBrush");
}
}
public Brush HighlightBrush
{
get
{
if(IsActive && IsReadOnly)
{
return ColorManager.CellActive;
}
if (IsCheckInvalid)
{
ColorAnimation animation = new ColorAnimation
{
From = Colors.Firebrick,
To = Colors.WhiteSmoke,
Duration = new Duration(TimeSpan.FromSeconds(1)),
AutoReverse = true
};
SolidColorBrush brush = new SolidColorBrush(Colors.Firebrick);
animation.RepeatBehavior = RepeatBehavior.Forever;
animation.AccelerationRatio = 0.5;
brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
return brush;
}
return new SolidColorBrush(Colors.Transparent);
}
}
public Brush BackgroundBrush
{
get
{
if (IsActive)
{
if (!IsReadOnly)
{
return ColorManager.CellActive;
}
}
if (IsInvalid)
{
return ColorManager.CellInvalid;
}
if (IsHighLighted)
{
return ColorManager.CellHighlighted;
}
return new SolidColorBrush(Colors.White);
}
}
[NonSerialized]
private Brush _backgroundAnimationBrush;
public Brush BackgroundAnimationBrush
{
get { return _backgroundAnimationBrush; }
set { _backgroundAnimationBrush = value; OnPropertyChanged("BackgroundAnimationBrush"); }
}
public Brush PossibilitiesBrush
{
get
{
return new SolidColorBrush(PossibilitiesColor);
}
}
private Colour _possibilitiesColor;
public Colour PossibilitiesColor
{
get
{
if (_possibilitiesColor == null)
{
return new Colour(Colors.Black);
}
return _possibilitiesColor;
}
set
{
_possibilitiesColor = value;
OnPropertyChanged("PossibilitiesColor");
OnPropertyChanged("PossibilitiesBrush");
}
}
public ObservableCollection<string> Possibilities
{
get { return model.Possibilities; }
set
{
model.Possibilities = value;
OnPropertyChanged("Possibilities");
OnPropertyChanged("PossibilityVisibility");
}
}
private string _toolTip;
public string ToolTip
{
get { return _toolTip; }
set { _toolTip = value; OnPropertyChanged("ToolTip"); }
}
public CellVM(Cell model,bool isHighlighted)
{
this.model = model;
IsValueMode = true;
IsHighLighted = isHighlighted;
}
public void signalError(string message)
{
ToolTip = message;
IsInvalid = true;
}
public void resetError()
{
if(IsCheckInvalid)
{
return;
}
ToolTip = null;
IsInvalid = false;
}
public void AnimateError()
{
ColorAnimation animation = new ColorAnimation
{
From = Colors.Firebrick,
To = Colors.Transparent,
Duration = new Duration(TimeSpan.FromSeconds(1.5)),
AutoReverse = false
};
animation.Completed += new EventHandler(animation_Completed);
SolidColorBrush brush = new SolidColorBrush(Colors.Transparent);
animation.AccelerationRatio = 0.5;
BackgroundAnimationBrush = brush;
brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
}
public void AnimateHint()
{
ColorAnimation animation = new ColorAnimation
{
From = Colors.DarkGreen,
To = Colors.Transparent,
Duration = new Duration(TimeSpan.FromSeconds(1.5)),
AutoReverse = false
};
animation.Completed += new EventHandler(animation_Completed);
SolidColorBrush brush = new SolidColorBrush(Colors.Transparent);
animation.AccelerationRatio = 0.5;
BackgroundAnimationBrush = brush;
brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
}
private void animation_Completed(object sender, EventArgs e)
{
BackgroundAnimationBrush = null;
}
}
CellVM SuperClass (ancestor):
[Serializable]
public abstract class ANotifyPropertyChanged : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
Try out marking event PropertyChanged by [NonSerialized] attribute
below is syntax for events: (see MSDN)
[field:NonSerializedAttribute()]
public event ChangedEventHandler Changed;

Categories

Resources