How to refresh the binding again using mvvmlight - wpf

The CurrentSelectedBall is updated whenever I changed its value due to two-way binding.
When I click to update the function UpdateRedBall is called so the redball in the database is updated. But the view list of balls is not updated as the ObservableCollection<RedBall> RedBalls is not changed at all.
How to fix this problem? I guess something needs to be done after _context.SaveChanges();
Also I can not simply do DataGridA.itemsSource = RedBalls to make a hard refresh here as first DataGridA is not accessible in the MainviewModel.
Some of the methods:
public ObservableCollection<RedBall> RedBalls
{
get { return new ObservableCollection<RedBall>(_context.RedBalls.OrderBy(m=>m.DateNumber));}
set
{
_redBalls = value;
RaisePropertyChanged("RedBalls");
}
}
public RedBall CurrentSelectedRedBall
{
get { return _currentSelectedRedBall; }
set
{
_currentSelectedRedBall = value;
RaisePropertyChanged("CurrentSelectedRedBall");
}
}
private void UpdateRedBall()
{
if (CurrentSelectedRedBall != null)
{
var ballToUpdate = _context.RedBalls.SingleOrDefault(x => x.Id == CurrentSelectedRedBall.Id);
ballToUpdate.DateNumber = CurrentSelectedRedBall.DateNumber;
ballToUpdate.First = CurrentSelectedRedBall.First;
ballToUpdate.Second = CurrentSelectedRedBall.Second;
ballToUpdate.Third = CurrentSelectedRedBall.Third;
ballToUpdate.Fourth = CurrentSelectedRedBall.Fourth;
ballToUpdate.Fifth = CurrentSelectedRedBall.Fifth;
ballToUpdate.Sixth = CurrentSelectedRedBall.Sixth;
}
_context.SaveChanges();
//RedBalls = RedBalls
}

I have a strong dislike for getters that 'do stuff'. It should be impossible for a get to fail. I would consider moving the logic in your RedBalls getter elsewhere. I'm also concerned that you're newing up the RedBalls ObservableCollection on every get as it can cause problems with bindings. Bound ObservableCollections should be instantiated once, then altered using Add, Remove and Clear. Working with them this way should also solve the problem of updating your collection appropriately.
public ObservableCollection<RedBall> RedBalls
{
get { return _redBalls; }
set
{
_redBalls = value;
RaisePropertyChanged("RedBalls");
}
}
public RedBall CurrentSelectedRedBall
{
get { return _currentSelectedRedBall; }
set
{
_currentSelectedRedBall = value;
RaisePropertyChanged("CurrentSelectedRedBall");
}
}
private void UpdateCurrentSelectedRedBall()
{
UpdateRedBall();
var redBalls = _context.RedBalls.OrderBy(m=>m.DateNumber);
if(redBalls != null)
{
RedBalls.Clear();
foreach(RedBall rb in redBalls)
{
RedBalls.Add(rb);
}
}
}
private void UpdateRedBall()
{
if (CurrentSelectedRedBall != null)
{
var ballToUpdate = _context.RedBalls.SingleOrDefault(x => x.Id == CurrentSelectedRedBall.Id);
ballToUpdate.DateNumber = CurrentSelectedRedBall.DateNumber;
ballToUpdate.First = CurrentSelectedRedBall.First;
ballToUpdate.Second = CurrentSelectedRedBall.Second;
ballToUpdate.Third = CurrentSelectedRedBall.Third;
ballToUpdate.Fourth = CurrentSelectedRedBall.Fourth;
ballToUpdate.Fifth = CurrentSelectedRedBall.Fifth;
ballToUpdate.Sixth = CurrentSelectedRedBall.Sixth;
}
_context.SaveChanges();
}

Use something like this: https://gist.github.com/itajaja/7507120 - basically you need to subscribe for PropertyChanged and raise CollectionChanged events from that.

Related

How to refresh Context in Entity framework

I cannot get updated item in ListView after modifying existing database item. Though, once I reload the application one can see updated item in ListView.
I have binded to an ObservableCollection for the ListView
This is my interface
public interface IService
{
IEnumerable<Employee> GetDetails();
IEnumerable<Employee> GetDetailsById(int MatchID);
}
I have implemented IService IEmployeeServiceData class.
public class IEmployeeServiceData:IService
{
private EmployeeContext Context
{
get;
set;
}
public IEmployeeServiceData()
{
Context = new EmployeeContext();
}
public IEnumerable<Model.Employee> GetDetails()
{
return Context.Employees;
}
public IEnumerable<Model.Employee> GetDetailsById(int MatchID)
{
var q = from f in Context.Employees
where f.EMPLOYEE_ID == MatchID
select f;
return q.AsEnumerable();
}
}
This is my VM
public void RefereshData()
{
var e = EmployeeService.GetDetails();
if (SelectedIndexValue == 1)
{
var Data = from e1 in e
where e1.LOCATION == "Pune"
select e1;
EmployeeMasterData = new ObservableCollection<Model.Employee>(Data);
}
else if(SelectedIndexValue==2)
{
var Data = from e1 in e
where e1.LOCATION == "Bangalore"
select e1;
EmployeeMasterData = new ObservableCollection<Model.Employee>(Data);
}
else
{
EmployeeMasterData = new ObservableCollection<Model.Employee>(e);
}
}
Updating Exiting Item:
public void UpdateEmployee()
{
try
{
Context = new EmployeeContext();
Employee Emp = Context.Employees.First(i => i.EMPLOYEE_ID == FindId1);
Emp.FIRST_NAME = this.FIRSTNAME;
Emp.FAMILY_NAME = this.FAMILY_NAME;
Emp.EXTERNAL_ID = this.EXTERNALID;
Emp.DB_SPONSOR = this.DBSPONSOR;
Emp.LEGAL_ENTITY = this.LEGAL_ENTITY;
Emp.COST_CENTER = this.COST_CENTER1;
Emp.STATUS = this.StatusCategory;
Emp.ENTRY_DATE = this.ENTRYDATE;
Emp.LOCATION = this.LocationCategory1;
Context.SaveChanges();
Clear();
AlertMessage1 = "Employee Record is Updated Sucessfulyy !!!";
IsVisible1 = true;
timer.Start();
}
catch(Exception ex)
{
Console.WriteLine(ex.InnerException);
}
}
Existing Item
Updated Item
Changes done to an entity in Entity Framework will not be reflected on screen because the two instances are not related in your example. Yes the have the same values, but they are two different distinct reference locations in memory. **For the ObservableCollection is a copy of the list and not the actual list being manipulated in your example.
Hence they are not related.
To show a change you have these options:
Change the actual object's property(ies) held by the observable collection to mirror the change done in to the other EF entity. Also the EF entity must adhere to INotifyPropertyChange or also the data property change won't be seen on the screen.
Or delete the screen entity and add the changed entity into the list.
Or delete the whole observable list and re-add it with the latest version from EF. (You mention that you do this and that is an option as well).

Entity Framework 6 Disable Interception temporarily

I am using an IDbCommandTreeInterceptor to enable soft deletes on my model.
System.Data.Entity.Infrastructure.Interception.DbInterception.Add(
new SoftDeleteInterception());
I want to be able to disable the interceptor temporarily so that I can select a "deleted" entity for auditing purposes.
However, It seems like the DbInterception collection is assembly-wide.
Is there any way to create a new DbContext without interception on?
Or even a way to add the interceptor to the DbContext every time it is created?
I have extended my db context class with additional property
[DbConfigurationType(typeof(DbConfig))]
public partial class YourEntitiesDB
{
public bool IgnoreSoftDelete { get; set; }
}
Then in the TreeCreated(...) method i check this flag and if true then it just doesn't go further to the QueryVisitor
public class SoftDeleteInterceptor : IDbCommandTreeInterceptor
{
public SoftDeleteInterceptor()
{
}
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext)
{
var db = interceptionContext.DbContexts.FirstOrDefault() as YourEntitiesDB;
if (db!=null && db.IgnoreSoftDelete)
{
// Ignore soft delete interseptor (Used in archives)
return;
}
if (interceptionContext.OriginalResult.DataSpace == DataSpace.CSpace)
{
var queryCommand = interceptionContext.Result as DbQueryCommandTree;
if (queryCommand != null)
{
var newQuery = queryCommand.Query.Accept(new SoftDeleteQueryVisitor());
interceptionContext.Result = new DbQueryCommandTree(
queryCommand.MetadataWorkspace,
queryCommand.DataSpace,
newQuery);
}
var deleteCommand = interceptionContext.OriginalResult as DbDeleteCommandTree;
if (deleteCommand != null)
{
var column = SoftDeleteAttribute.GetSoftDeleteColumnName(deleteCommand.Target.VariableType.EdmType);
if (column != null)
{
var setClauses = new List<DbModificationClause>();
var table = (EntityType)deleteCommand.Target.VariableType.EdmType;
if (table.Properties.Any(p => p.Name == column))
{
setClauses.Add(DbExpressionBuilder.SetClause(
DbExpressionBuilder.Property(
DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName),
column),
DbExpression.FromBoolean(true)));
}
var update = new DbUpdateCommandTree(
deleteCommand.MetadataWorkspace,
deleteCommand.DataSpace,
deleteCommand.Target,
deleteCommand.Predicate,
setClauses.AsReadOnly(),
null);
interceptionContext.Result = update;
}
}
}
}
}
In order to use it i just set the flag to true when needed
YuorEntitiesDB DB = new YuorEntitiesDB();
DB.IgnoreSoftDelete = true;
DB.Records.Where(...)

Do event after PropertyChanged Event is done

I am trying to do a "Clear Flag" in my code. I have data being shown on the UI continuously, and I want the user to be able to clear the data when the code is running with a "Clear" button. I have a second thread collecting and updating the data on the UI Thread. I tried to use a flag in the properties that when it sees the "Clear Flag" is true to rests to a predetermined value. When the clear button is pressed I set my "Clear Flag" to true and it clears the data, but my problem is knowing when it is done and setting the "Clear Flag" back to false when it is done.
private bool _clear;
public bool Clear
{
get
{
return _clear;
}
set
{
_clear = value;
OnPropertyChanged("Clear");
if (value)
{
OnPropertyChanged(String.Empty);
}
}
}
private int _motorRPM;
public int MotorRPM
{
get
{
return _motorRPM;
}
set
{
_motorRPM = value;
OnPropertyChanged("MotorRPM");
}
}
private int _aveRPM;
public int AveRPM
{
get
{
return _aveRPM;
}
set
{
if (Clear)
{
_aveRPM = 0;
}
else
{
_aveRPM = (_aveRPM + value) / 2;
}
OnPropertyChanged("AveRPM");
}
}
private int _minRPM = Int32.MaxValue;
public int MinRPM
{
get
{
return _minRPM;
}
set
{
if (Clear)
{
_minRPM = Int32.MaxValue;
}
else
{
if (value < _minRPM)
{
_minRPM = value;
}
}
OnPropertyChanged("MinRPM");
}
}
private int _maxRPM;
public int MaxRPM
{
get
{
return _maxRPM;
}
set
{
if (Clear)
{
_maxRPM = 0;
}
else
{
if (value > _maxRPM)
{
_maxRPM = value;
}
}
OnPropertyChanged("MaxRPM");
}
}
As you can see I have a "Clear" Property that when set to true will call an update all properties with OnPropertyChanged(String.Empty) with each property resting to a known value when "Clear" is true.
How do I set Clear back to false when all PropertyChanged events are done?
As said Niclas, I would said: do not do it like this! The purpose of the PropertyChanged in your ViewModel is to update the UI. If you had some sort of logic depending of your propertychanged, it will be a nightmare to debug!
Keep your property as clean as possible. Now to update some dependent value it would be better to do something like this:
private bool _clear;
public bool Clear
{
get
{
return _clear;
}
set
{
_clear = value;
OnPropertyChanged("Clear");
if (_clear)
ClearValues();
}
}
public void ClearValues()
{
AveRPM=0;
MinRPM=0;
MaxRPM=0;
...
Clear=False;
}
Not perfect but seems much more readable and maintainable to me.
Hope it helps.

Proper use of DifferentDatabaseScope when it's within another SessionScope

I have the following code (simplified for brevity)
[ActiveRecord(Table = "[Order]"),
Serializable]
public class Order : ActiveRecordLinqBase<Order>, INotifyPropertyChanged
{
//Other properties omitted for brevity
private IList<Reading> _readings;
[HasMany(Inverse = true, Cascade = ManyRelationCascadeEnum.All)]
public virtual IList<Reading> Readings
{
get { return _readings; }
set
{
_readings = value;
DoPropertyChanged(() => this.Readings);
}
}
}
using (new SessionScope(FlushAction.Never) {
Order o = _repository.GetOrder(OrderId);
bool result = ShowReadings(this, o);
if (result) s.Flush();
}
bool ShowReadings(Window owner, Order o) {
//populate a datagrid with Reading objects from Order
this.Readings = Order.Readings //datagrid binds to Readings property of the form
if (cbxHistoricalChecked) {
using(new DifferentDatabaseScope(SomeOtherConnectionString)) {
//bind datagrid to historical readings
this.Readings = GetHistoricalReadings(Order.OrderId);
}
}
if (SignedOff) {
using(var t = new TransactionScope()) {
//update some properties on the Order
t.VoteCommit();
}
}
return true;
}
So what I'm doing is having a SessionScope open while the readings form is open, and flushed once it is closed. If the user wants to view historical readings for the order, I have to open a different database for the historical readings.
Things work fine as long as I don't open the DifferentDatabaseScope, but when I do, the creation of a new TransactionScope fails:
Castle.ActiveRecord.Framework.Scopes.ScopeMachineryException {"Tried to unregister a scope that is not the active one"}
Is it possible to do what I'm trying to do, and if so what is the proper way to do it?

Is there a nullable datepicker that I can bind to?

I am looking for a datepicker like what microsoft provides, but it doesn't support null values, and since this is tied to a nullable field on a database that isn't acceptable.
I found this one, but according to the comments at the bottom of the page it has issues with binding to a database. I also have one in my project that I inherited, but it has similar issues (sometimes it shows values, sometimes it doesn't). Does anyone know of one that works?
Use a date picker to populate a textbox and if they want the field to be null, just erase the contents of the textbox (and then handle the blank input accordingly).
This also provides the added benefit of allowing the user to type in their date if they so choose.
Smart FieldPackEditor has a datepicker that is nullable. I believe it does everything that you need. I wish this was around when I was dealing with this sort of stuff. I still remember all the workarounds I had to implement with Microsoft's datepicker control. Uggh!
http://www.visualhint.com/index.php/fieldpackeditor/
why not use a client side datepicker to populate a text field. If the textfield is empty, then you have a null date, otherwise convert the value.
jQuery has a nice easy to use datepicker. http://jqueryui.com
This one seems to work, one of my co-workers had it:
using System;
using System.Windows.Forms;
namespace CustomControls
{
public class NullableBindableDateTimePicker : System.Windows.Forms.DateTimePicker
{
private Boolean isNull = false;
private DateTimePickerFormat baseFormat = DateTimePickerFormat.Short;
private Boolean ignoreBindOnFormat = false;
public NullableBindableDateTimePicker()
{
this.Format = baseFormat;
if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
}
public Boolean IsNull
{
get { return isNull; }
set
{
isNull = value;
this.Checked = value;
}
}
//TODO: Add BaseCustomFormat
public DateTimePickerFormat BaseFormat
{
get { return baseFormat; }
set { baseFormat = value; }
}
public object BindMe
{
get
{
if (IsNull) return System.DBNull.Value;
else return base.Value;
}
set
{
//String s = this.Name;
if (ignoreBindOnFormat) return;
if (System.Convert.IsDBNull(value))
{
// for some reason setting base.format in this.format calls set BindMe.
// we need to ignore the following call
ignoreBindOnFormat = true;
this.Format = DateTimePickerFormat.Custom;
ignoreBindOnFormat = false;
this.CustomFormat = " ";
IsNull = true;
}
else
{
ignoreBindOnFormat = true;
this.Format = baseFormat;
ignoreBindOnFormat = false;
if (baseFormat == DateTimePickerFormat.Custom) this.CustomFormat = " ";
IsNull = false;
base.Value = (DateTime)value;
}
}
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Delete)
{
this.BindMe = DBNull.Value;
}
}
protected override void OnCloseUp(EventArgs eventargs)
{
base.OnCloseUp(eventargs);
BindMe = base.Value;
}
}
}

Resources