I have the following:
var db = new datesDataContext();
var query =
from ord in db.Dates
where ord.id == id
select ord;
foreach (Date ord in query)
{
ord.date1 = product.date1;
ord.name = product.name;
}
db.SubmitChanges();
It all runs fine (no errors, etc) except that SubmitChanges is not making the changes in the database.
ord.dat1 and ord.name are definitely being set...
edit: here's my date class (it says partial but it's the entire class, no other definition elsewhere):
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Dates")]
public partial class Date
{
private System.Nullable<int> _id;
private System.Nullable<System.DateTime> _date1 = DateTime.Now;
private string _name;
public Date()
{
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_id", DbType="Int")]
public System.Nullable<int> id
{
get
{
return this._id;
}
set
{
if ((this._id != value))
{
this._id = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Name="date", Storage="_date1", DbType="DateTime")]
public System.Nullable<System.DateTime> date1
{
get
{
return this._date1;
}
set
{
if ((this._date1 != value))
{
this._date1 = value;
}
}
}
[global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_name", DbType="Text", UpdateCheck=UpdateCheck.Never)]
public string name
{
get
{
return this._name;
}
set
{
if ((this._name != value))
{
this._name = value;
}
}
}
}
Is your Date class implementing INotifyPropertyChanged?
EDIT: implement INotifyPropertyChanged in your Date class
public partial class Date : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
.....rest of class definition
and in each setter method use this code after changing values
NotifyPropertyChanged("PropertyName");
PropertyName in your case is id, date1 and name.
You also need to specify Primary Key otherwise tracking changes will not work.
This should work. Something must be wrong with either your configuration or your model classes. Can you include code from the Date class as well?
I'm seeing this next to your "name" property: UpdateCheck=UpdateCheck.Never
I'm guessing this setting is causing that property to not update.
Related
Currently I want the calculations to be dynamic and change on per key stroke. I am trying to do this using MVVM but not entirely sure how.
In the view Model:
public int? Duration { get { return _seb.Duration; } set { _seb.Duration = value;} }
public decimal? Amount { get { return _seb.AmountPer; } set { _seb.AmountPer = value;} }
I have a total Variable And would like it to be constantly updated. May I ask how do I do this.
I tried something like this but no luck
public decimal? Total {get { return _seb.Total; } set { _seb.Total = Amount*Duration; }}
This can be done by raising the ProperyChanged event of the total property when either of the other two property changes.
public class SomeViewModel : ViewModelBase
{
private int? _duration;
private decimal? _amount;
public int? Duration
{
get { return _duration; }
set
{
if (_duration != value)
{
_duration = value;
RaisePropertyChanged("Duration");
RaisePropertyChanged("Total");
}
}
}
public decimal? Amount
{
get { return _amount; }
set
{
if (_amount != value)
{
_amount = value;
RaisePropertyChanged("Amount");
RaisePropertyChanged("Total");
}
}
}
public decimal? Total
{
get
{
if (Amount.HasValue && Duration.HasValue)
return Amount.Value * Duration.Value;
return null;
}
}
}
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
You must implement INotifyPropertyChanged and put the formula on get of the total property
public int? Duration
{
get
{
return _seb.Duration;
}
set
{
_seb.Duration = value;
this.RaisePropertyChanged("Total")
}
}
You should show your xaml view as well.
Assuming you are using a textbox, bound to some property in your ViewModel, you can make sure that that property is updated whenever the textbox propery changes by doing something like this:
<TextBox Text={Binding MyVmProperty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged} />
I'm new to Windows Phone 7 development and I'm in trouble with Linq to SQL. I'm trying to update an object but it just don't work. I get the object I want to update and modify the property I want, but when I call SaveChanges, the data it's not updated on database.
I already downloaded the database using ISETool and checked that data isn't updated at all.
What's strange is the querying and inserting methods works fine, but I don't know why updating it's not.
Here's the entity and the updating method code:
[Table]
public class Entrada : INotifyPropertyChanged, INotifyPropertyChanging
{
private int _Id;
[Column]
private int _DiaId;
[Column]
private int _ProjetoId;
private EntityRef<Dia> _Dia;
private EntityRef<Projeto> _Projeto;
private DateTime _Chegada;
private DateTime? _Saida;
[Column(IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false, DbType = "INT NOT NULL Identity")]
public int Id
{
get
{
return _Id;
}
set
{
if (value != _Id)
{
NotifyPropertyChanging("Id");
_Id = value;
NotifyPropertyChanged("Id");
}
}
}
[Column(CanBeNull=false)]
public DateTime Chegada
{
get
{
return _Chegada;
}
set
{
if (value != _Chegada)
{
_Chegada = value;
NotifyPropertyChanged("Chegada");
}
}
}
[Association(Storage = "_Dia", ThisKey = "_DiaId", OtherKey="Id", IsForeignKey=true)]
public Dia Dia
{
get { return _Dia.Entity; }
set
{
NotifyPropertyChanging("Dia");
_Dia.Entity = value;
if (value != null)
{
_DiaId= value.Id;
}
NotifyPropertyChanging("Dia");
}
}
[Column(CanBeNull=true)]
public DateTime? Saida
{
get
{
return _Saida;
}
set
{
if (value != _Saida)
{
_Saida = value;
NotifyPropertyChanged("Saida");
}
}
}
[Association(Storage = "_Projeto", ThisKey = "_ProjetoId", OtherKey = "Id", IsForeignKey = true)]
public Projeto Projeto
{
get
{
return _Projeto.Entity;
}
set
{
NotifyPropertyChanging("Projeto");
_Projeto.Entity = value;
if (value != null)
{
_ProjetoId = value.Id;
}
NotifyPropertyChanging("Projeto");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public event PropertyChangingEventHandler PropertyChanging;
private void NotifyPropertyChanged(String propertyName)
{
if (null != PropertyChanged)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}
}
//UPDATE CODE:
var query = (from e in timeSheetDB.Entradas where e.Dia.Id == this.Dia.Id && (!e.Saida.HasValue) select e);
var entrada = query.FirstOrDefault();
if (entrada != null)
{
entrada.Saida = DateTime.Now;
}
timeSheetDB.SubmitChanges();
I also checked the GetChangeSet().Updates.Count(), but it's always 0. I hope you can help me :-)
thank you guys!
It appears to be the case that you're not raising the PropertyChanging event for the Saida property; this event is important for LINQ-to-SQL, so I'd suggest updating all of your members that represent columns to raise it (in addition to the PropertyChanged event)
I have a listbox to which I'm binding HistoryItems , where HistoryItems is a ObservableCollection of History.
Here is the listbox declaration :
<ListBox x:Name="RecentListBox" SelectionChanged="RecentListBox_SelectionChanged" ItemsSource="{Binding HistoryItems,Converter={StaticResource HistoryValueConverter} }" ItemsPanel="{StaticResource ItemsPanelTemplate1_Wrap}" ItemTemplate="{StaticResource RecentViewModelTemplate}">
Here is the History class :
public class History : INotifyPropertyChanged
{
public History() { }
int _id;
string _date;
string _url;
string _name;
public History(int id, string date,string url,string name)
{
this.id = id;
this.date = date;
this.url = url;
this.name = name;
}
public int id
{
get
{
return _id;
}
set
{
if (value != _id)
{
_id = value;
NotifyPropertyChanged("id");
}
}
}
public string date
{
get
{
return _date;
}
set
{
if (!value.Equals(_date))
{
_date = value;
NotifyPropertyChanged("string");
}
}
}
public string url
{
get
{
return _url;
}
set
{
if (!value.Equals(_url))
{
_url = value;
NotifyPropertyChanged("url");
}
}
}
public string name
{
get
{
return _name;
}
set
{
if (!value.Equals(_name))
{
_name = value;
NotifyPropertyChanged("name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
// App.viewModel.HistoryItems = (App.Current as App).dataHandler.retrieveHistory_DB();
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
When I start the app the list gets populated, but after I do some modifs in some pivot children, and go back to the main panorama view, I try to update the HistoryItems in OnNavigatedTo :
App.ViewModel.HistoryItems = (App.Current as App).dataHandler.retrieveHistory_DB();
but the listbox doesn't get updated (and the function returns the correct data). What could the problem be? History is INotifyPropertyChanged and the HistoryItems is a ObservableCollection<History> so there should be no problem.. What is causing the list to not update?
Since you are replacing HistoryItems when you refresh it doesn't matter that it's an ObservableCollection.
You can either clear the HistoryItems and then add the new items when you refresh. Or the ViewModel should implement INotifyPropertyChanged and the HistoryItems setter should raise the event.
Rewrite the setter for ViewModel.HistoryItems so that instead of doing this
_historyItems = value;
it does this
if (_historyItems == null)
_historyItems = new ObservableCollection<HistoryItem>();
_historyItems.Clear();
foreach (var hi in value)
_historyItems.Add(hi);
You need NotifyPropertyChanged for App.ViewModel in the Setter of HistoryItems
I've a class like this:
public class PersonViewModel : ViewModelBase //Here is the INotifyPropertyChanged Stuff
{
public PersonViewModel(Person person)
{
PersonEntity = person;
}
public Person PersonEntity {
get { return PersonEntity.Name; }
private set { PersonEntity.Name = value; RaisePropertyChanged("PersonEntity");
}
public string Name {
get { return PersonEntity.Name; }
set { PersonEntity.Name = value; RaisePropertyChanged("Name");
}
public int Age{
get { return PersonEntity.Age; }
set { PersonEntity.Age= value; RaisePropertyChanged("Age");
}
public void ChangePerson(Person newPerson)
{
//Some Validation..
PersonEntity = newPerson;
}
My TextBoxes are bound to Name and Age of the ViewModel.
If I change the _person object in the ViewModel, do I have to call for each Property a RaisePropertyChanged again or is there a way to do this automaticly (in my concret example I have about 15 Properties..)?
Thanks for any help.
Cheers
Joseph
You can indicate all properties have changed by using null or string.Empty for the property name in PropertyChangedEventArgs. This is mentioned in the documentation for PropertyChanged.
One other solution I used to tackle the problem of: first setting the value and then calling the PropertyChangedEventArgs is by adding a Set function in my ViewModelBase which looks like this:
public class ViewModelBase : INotifyPropertyChanged
{
protected bool Set<T>(ref T backingField, T value, [CallerMemberName] string propertyname = null)
{
// Check if the value and backing field are actualy different
if (EqualityComparer<T>.Default.Equals(backingField, value))
{
return false;
}
// Setting the backing field and the RaisePropertyChanged
backingField = value;
RaisePropertyChanged(propertyname);
return true;
}
}
Instead of doing this:
public string Name {
get { return PersonEntity.Name; }
set { PersonEntity.Name = value; RaisePropertyChanged("Name");
}
You can now achieve the same by doing this:
public string Name {
get { return PersonEntity.Name; }
set { Set(ref PersonEntity.Name,value);
}
In .NET I have a class called Caption. I have another class called Gauge. Within the Gauge class I have a property defined as a Caption.
I am trying to figure out how to do the following:
When a certain property is changed in my Caption class how do I get it to execute a subroutine in the Gauge class? I am thinking I have to declare an event and AddHandlers to fire it off, but I can't think of how to accomplish this.
You'll want to look at implementing the INotifyPropertyChanged interface, which is designed exactly for the purpose - raising an event when a property of a class instance changes.
A good example of usage is given on this MSDN page.
// This class implements a simple customer type
// that implements the IPropertyChange interface.
public class DemoCustomer : INotifyPropertyChanged
{
// These fields hold the values for the public properties.
private Guid idValue = Guid.NewGuid();
private string customerName = String.Empty;
private string companyNameValue = String.Empty;
private string phoneNumberValue = String.Empty;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
// The constructor is private to enforce the factory pattern.
private DemoCustomer()
{
customerName = "no data";
companyNameValue = "no data";
phoneNumberValue = "no data";
}
// This is the public factory method.
public static DemoCustomer CreateNewCustomer()
{
return new DemoCustomer();
}
// This property represents an ID, suitable
// for use as a primary key in a database.
public Guid ID
{
get
{
return this.idValue;
}
}
public string CompanyName
{
get {return this.companyNameValue;}
set
{
if (value != this.companyNameValue)
{
this.companyNameValue = value;
NotifyPropertyChanged("CompanyName");
}
}
}
public string PhoneNumber
{
get { return this.phoneNumberValue; }
set
{
if (value != this.phoneNumberValue)
{
this.phoneNumberValue = value;
NotifyPropertyChanged("PhoneNumber");
}
}
}
}
public class Caption
{
private int myInt;
public event EventHandler MyIntChanged;
private void OnMyIntChanged()
{
var handler = this.MyIntChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
public int MyInt
{
get
{
return this.myInt;
}
set
{
if (this.myInt != value)
{
this.myInt = value;
this.OnMyIntChanged();
}
}
}
}
So now, in your guage class:
public class Guage
{
private Caption caption;
public Caption Caption
{
get
{
return this.caption;
}
set
{
if (this.caption!= value)
{
this.caption= value;
this.caption.MyIntChanged += new EventHandler(caption_MyIntChanged);
}
}
}
private void caption_MyIntChanged(object sender, EventArgs e)
{
//do what you gotta do
}
}