How to compare latest input with initial value using PropertyChanged? - c#

I have class in xamarin form app and I implemented INotifyChanged on it. Later this class will be used to create a list. The data for the list comes from mysql. The list will be the item source for my third party dataGrid called sfdataGrid. The column actual reading will display integer from db and can be edited in the data grid. But the newly edited value must be bigger than the value initial value from db. Or it should revert back to initial value. How can compare the new value with initial value property Changed?
public class actualmeterreading : INotifyPropertyChanged
{
private string _ID;
private string _MachineMeterReadingID;
private Int32 _ActualReading;
private machinemeterreadinglist _MachineMeterReadingList;
public actualmeterreading(string id, string machinemeterreadingid, Int32 actualreading, machinemeterreadinglist machinemeterreadinglist)
{
this._ID = id;
this._MachineMeterReadingID = machinemeterreadingid;
this._ActualReading = actualreading;
this._MachineMeterReadingList = machinemeterreadinglist;
}
public actualmeterreading()
{
this._ID = string.Empty;
this._MachineMeterReadingID = string.Empty;
this._ActualReading = 0;
this._MachineMeterReadingList = new machinemeterreadinglist();
}
public string ID
{
get { return _ID;}
set { _ID = value;}
}
public string MachineMeterReadingID
{
get { return _MachineMeterReadingID;}
set { _MachineMeterReadingID = value;}
}
public int ActualReading
{
get { return _ActualReading;}
set {
_ActualReading = value;
RaisePropertyChanged("ActualReading");
}
}
public machinemeterreadinglist MachineMeterReadingList
{
get { return _MachineMeterReadingList;}
set { _MachineMeterReadingList = value;}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(String Name)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(Name));
}
}

public int ActualReading
{
get { return _ActualReading;}
set {
// only update if new value is bigger than old value
if (value > _ActualReading) {
_ActualReading = value;
RaisePropertyChanged("ActualReading");
}
}
}

Related

Little complex Databinding in windows form combobox

I am in trouble while working on Databinding in windows form. I have two classes, one is Project and another is Update. Now all project object is having a list of Updates and it is binded to a combobox, but when the user changes selection need to display/bind the properties of Update object to another controls. But is not updating as expected, when the user changes selection. Please help me on this..
Screenshot
See my class and codes below,
public class Project
{
private int _id;
private string _name;
public Project(int id, string name)
{
_id = id;
_name = name;
ReadUpdates();
}
public List<Update> AvailableUpdates { get; set; }
public int Id { get { return _id; } }
public string ProjName
{
get { return _name; }
set { _name = value; }
}
private void ReadUpdates()
{
AvailableUpdates = new List<Update>();
for (int i = 0; i < 10; i++)
{
AvailableUpdates.Add(new
Update(i, DateTime.Now.AddDays(i)));
}
}
}
public class Update
{
private string _title;
private int _uid;
private DateTime _updatedOn;
public Update(int id, DateTime updatedOn)
{
_title = $"Update:{id}";
_uid = id;
_updatedOn = updatedOn;
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public int UId
{
get { return _uid; }
set { _uid = value; }
}
public DateTime UpdatedOn
{
get { return _updatedOn; }
set { _updatedOn = value; }
}
}
public partial class Form1 : Form
{
private Update _currentUpdate;
private Project _project;
public Form1()
{
InitializeComponent();
_project = new Project(1, "Sample Project");
DoBindings();
}
private void DoBindings()
{
NameBox.DataBindings.Add("Text", _project, "ProjName");
IdBox.DataBindings.Add("Text", _project, "Id");
UpdatesCombo.DataSource = _project.AvailableUpdates;
UpdatesCombo.DisplayMember = "UId";
_currentUpdate = (Update)UpdatesCombo.SelectedItem;
UpdateTitle.DataBindings.Add("Text", _currentUpdate, "Title");
UpdateDate.DataBindings.Add("Value", _currentUpdate, "UpdatedOn");
}
private void UpdatesCombo_SelectionChangeCommitted(object sender, System.EventArgs e)
{
_currentUpdate = (Update)UpdatesCombo.SelectedItem;
}
}
Please correct me if I am wrong.
It's quite simple. All you need is to bind the related controls to the same list data source as the combo box.
var updates = _project.AvailableUpdates;
UpdatesCombo.DataSource = updates;
UpdatesCombo.DisplayMember = "UId";
UpdateTitle.DataBindings.Add("Text", updates, "Title");
UpdateDate.DataBindings.Add("Value", updates, "UpdatedOn");
The data binging infrastructure creates CurrencyManager class per each unique list data source. The non list controls are actually bound to the Current property, which is updated by the combo box selection.

Bind individual elements of collection in WPF application

It is WPF application and I’m trying to bind individual collection item property in TextBlock. I search on StackOverflow and many others have asked similar questions and they have their solution working. I tried to access value same way but somehow, it’s not displaying Index value in my case so posting similar question. Please help me to identify what I'm doing wrong here.
View model
public class SequeanceViewModel
{
public ObservableCollection<Sequence> SequenceList = new ObservableCollection<ViewModel.Sequence>();
public SequeanceViewModel()
{
for (int i = 1; i <= 6; i++)
{
SequenceList.Add(new ViewModel.Sequence() { Index = i, Name = "Name goes here" });
}
}
}
public class Sequence : INotifyPropertyChanged
{
private int index { get; set; }
private bool current { get; set; }
private string name;
public int Index
{
get
{
return index;
}
set
{
index = value;
OnPropertyChanged(new PropertyChangedEventArgs("Index"));
}
}
public bool Current
{
get
{
return current;
}
set
{
current = value;
OnPropertyChanged(new PropertyChangedEventArgs("Current"));
}
}
public string Name
{
get
{
return name;
}
set
{
name = value;
OnPropertyChanged(new PropertyChangedEventArgs("Name"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
}
Window code
SequeanceViewModel sequeanceViewModel;
public Validation()
{
InitializeComponent();
sequeanceViewModel = new SequeanceViewModel();
this.DataContext = sequeanceViewModel;
}
Binding in xaml
<TextBlock Text="{Binding SequenceList[0].Index, Mode=OneWay}"></TextBlock>
Since you can only bind to public properties, you must define SequenceList as a property and not as a public field:
public ObservableCollection<Sequence> SequenceList { get; } = new ObservableCollection<ViewModel.Sequence>();
You must expose SequenceList as a property instead of a public variable. Otherwise you cannot bind to it.

listbox data binding fails silently

This approach seems to work half the time for me.
I can see these lines get executed in the debugger:
agencyListBox.DataBindings.Add(new Binding("DataSource", this.Data.Agencies, "AvailableAgencies"));
agencyListBox.DataBindings.Add(new Binding("SelectedItem", this.Data.Agencies, "SelectedAgency", false, DataSourceUpdateMode.OnPropertyChanged));
The agencies class looks like this:
public AgencyType SelectedAgency
{
get
{
return _selected;
}
set
{
_selected = value;
OnPropertyChanged("SelectedAgency");
}
}
public List<AgencyType> AvailableAgencies
{
get
{
return _availableList;
}
set
{
_availableList = value;
OnPropertyChanged("AvailableAgencies");
}
}
So the fields I reference in the binding do exist.
The DisplayMember is set to "Label" which is defined in the AgencyType class:
public event PropertyChangedEventHandler PropertyChanged;
private string _label { get; set; }
public string Label
{
get { return _label; }
set
{
_label = value;
OnPropertyChanged("Label");
}
}
private string _identifier { get; set; }
public string Identifier
{
get { return _identifier; }
set
{
_identifier = value;
OnPropertyChanged("Identifier");
}
}
public AgencyType()
{
Label = string.Empty;
Identifier = string.Empty;
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
The values are displayed as desired.
But then when I change the selection, data.Agencies.SelectedAgency is null!
Does anyone have any tips?

Empty local database after filling it

I'm using local database to store my data in Windows Phone 8 application. At first I have data stored in JSON object which is converted to my classes objects and then collection of these objects I try to store in local database. I was checking in debug mode and data is in those objects, but when I check database, it's empty.
This is how I move data from collection to database:
// Data context for the local database
private TablesDataContext tablesDB;
// Define the query to gather all of items.
var customersTablesInDB = from CustomerItem todo in tablesDB.CustomersTable
select todo;
// Execute the query and place the results into a collection.
CustomersTable = new ObservableCollection<CustomerItem>(customersTablesInDB);
foreach (Customer customer in customersList)
{
// Create a new item
CustomerItem newCustomer = new CustomerItem
{
Id = customer.id,
Number = customer.number.Value,
Name = customer.name,
Email = customer.email
};
// Add item to the observable collection.
CustomersTable.Add(newCustomer);
// Add item to the local database.
tablesDB.CustomersTable.InsertOnSubmit(newCustomer);
}
Here is my class for DataContext:
public class TablesDataContext : DataContext
{
// Specify the connection string as a static, used in main page and app.xaml.
public static string DBConnectionString = "Data Source=isostore:/Customers.sdf";
// Pass the connection string to the base class.
public TablesDataContext(string connectionString)
: base(connectionString)
{ }
// Specify a single table for the items.
public Table<CustomerItem> CustomersTable;
}
And here is my CustomerItem class:
[Table]
public class CustomerItem : INotifyPropertyChanged, INotifyPropertyChanging
{
// Define ID: private field, public property and database column.
private int _id;
[Column(IsPrimaryKey = true, IsDbGenerated = false, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
public int Id
{
get
{
return _id;
}
set
{
if (_id != value)
{
NotifyPropertyChanging("Id");
_id = value;
NotifyPropertyChanged("Id");
}
}
}
// Define item name: private field, public property and database column.
private int? _number;
[Column]
public int? Number
{
get
{
return _number;
}
set
{
if (_number != value)
{
NotifyPropertyChanging("Number");
_number = value;
NotifyPropertyChanged("Number");
}
}
}
// Define completion value: private field, public property and database column.
private String _name;
[Column]
public String Name
{
get
{
return _name;
}
set
{
if (_name != value)
{
NotifyPropertyChanging("Name");
name = value;
NotifyPropertyChanged("Name");
}
}
}
// Define completion value: private field, public property and database column.
private String _email;
[Column]
public String Email
{
get
{
return _email;
}
set
{
if (_email != value)
{
NotifyPropertyChanging("Email");
_email = value;
NotifyPropertyChanged("Email");
}
}
}
// Version column aids update performance.
[Column(IsVersion = true)]
private Binary _version;
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
// Used to notify the page that a data context property changed
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region INotifyPropertyChanging Members
public event PropertyChangingEventHandler PropertyChanging;
// Used to notify the data context that a data context property is about to change
private void NotifyPropertyChanging(String propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
#endregion
}
You are missing a "tablesDB.SubmitChanges()" after your foreach-loop.

Update in sql Ce in window phone 7

In project i user SQL CE, i have Table:
[Table]
public class Article : INotifyPropertyChanged, INotifyPropertyChanging
{
// Define _cid: private field, public property, and database column.
private int _aid;
[Column(DbType = "INT NOT NULL IDENTITY", IsDbGenerated = true, IsPrimaryKey = true)]
public int aid
{
get { return _aid; }
set
{
NotifyPropertyChanging("aid");
_aid = value;
NotifyPropertyChanged("aid");
}
}
// Define nameColor name: private field, public property, and database column.
private int _rid;
[Column]
public int rid
{
get { return _rid; }
set
{
NotifyPropertyChanging("rid");
_rid = value;
NotifyPropertyChanged("rid");
}
}
private string _title;
[Column]
public string title
{
get { return _title; }
set
{
NotifyPropertyChanging("title");
_title = value;
NotifyPropertyChanged("title");
}
}
private string _thumnail;
[Column]
public string thumnail
{
get { return _thumnail; }
set
{
NotifyPropertyChanging("thumnail");
_thumnail = value;
NotifyPropertyChanged("thumnail");
}
}
private string _DesScription;
[Column(DbType = "NTEXT")]
public string DesScription
{
get { return _DesScription; }
set
{
NotifyPropertyChanging("DesScription");
_DesScription = value;
NotifyPropertyChanged("DesScription");
}
}
private int _orderID;
[Column]
public int orderID
{
get { return _orderID; }
set
{
NotifyPropertyChanging("orderID");
_orderID = value;
NotifyPropertyChanged("orderID");
}
}
private string _pubDate;
[Column]
public string pubDate
{
get { return _pubDate; }
set
{
NotifyPropertyChanging("pubDate");
_pubDate = value;
NotifyPropertyChanged("pubDate");
}
}
private string _linkURL;
[Column]
public string linkURL
{
get { return _linkURL; }
set
{
NotifyPropertyChanging("linkURL");
_linkURL = value;
NotifyPropertyChanged("linkURL");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
// Used to notify that a property changed
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region INotifyPropertyChanging Members
public event PropertyChangingEventHandler PropertyChanging;
// Used to notify that a property is about to change
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
#endregion
}
when i Update Colum Thumnail , i have erros :
SQL Server does not handle comparison of NText, Text, Xml, or Image data types
because of special characters into database insert sequence trogn should I use BbType = "NTEXT"
Please Help me !
You could remove this column for concurrency checking by adding [Column(UpdateCheck = UpdateCheck.Never)] to this column.
See this blogpost about Linq to sql concurrency checking: http://blogs.msdn.com/b/matt/archive/2008/05/22/into-to-linq-to-sql-optimistic-concurrency.aspx

Categories

Resources