I am binding a button in my XAML to update a user in my database. I want to utilize CanExecute to enable the button only after a change has been made to a property on the ViewModel.
I am using the Entity Framework and a WCF Data service to perform my CRUD operations.
Model Class
public virtual DbSet<user> users { get; set; }
...
namespace pk.Data
{
using System;
using System.Collections.Generic;
public partial class user
{
public user()
{
this.private_account = new HashSet<private_account>();
}
public int id { get; set; }
public string username { get; set; }
public int superuser { get; set; }
public int enabled { get; set; }
public virtual ICollection<private_account> private_account { get; set; }
}
}
WCF Service
public class pkService
{
// getUsers() -
// Get all users from the database
[OperationContract]
public IEnumerable<user> getUsers()
{
using (var context = new pkEntities())
{
// Needed in order to handle EF objects with Hashes
context.Configuration.ProxyCreationEnabled = false;
// Pull entity object fom DB
var results = context.users.ToList();
// Detatch from Entity Model
results.ForEach(e => context.Entry(e).State = EntityState.Detached);
//Return detached object
return results;
}
}
// updateUser(user) -
// Update user in the DB
[OperationContract]
public void updateUser(user user){
using (var context = new pkEntities())
{
context.Configuration.ProxyCreationEnabled = false;
context.Entry(user).State = EntityState.Modified;
context.SaveChanges();
}
}
}
View Model #
public class HomeViewModel: ViewModelBase
{
// HomeViewModel()
// Class Constructor
public HomeViewModel()
{
this.RefreshUsers();
userListClick = new RelayCommand<SelectionChangedEventArgs>(_userListClick);
saveUser = new RelayCommand(saveUser);
}
// users
// list of all users in the system
private IEnumerable<user> _users;
public IEnumerable<user> users
{
get { return this._users; }
set
{
this._users = value;
this.RaisePropertyChanged("users");
}
}
// selected user
// currently selected user
private user _selectedUser;
public user selectedUser
{
get { return this._selectedUser; }
set
{
if (this._selectedUser != value)
{
this._selectedUser = value;
this.RaisePropertyChanged("selectedUser");
}
}
}
// RefreshUsers()
// Method for retrieving users from DB
private void RefreshUsers()
{
this._pkServiceClient.getUsersCompleted += (s, e) =>
{
this.users = e.Result;
};
this._pkServiceClient.getUsersAsync();
}
// serverListClick
public ICommand userListClick { get; private set; }
private void _userListClick(SelectionChangedEventArgs e)
{
ListView userList = (ListView)e.Source;
_selectedUser = (user)userList.SelectedItem;
this.setUserDetailsVisible(Visibility.Visible);
this.RaisePropertyChanged("selectedUser");
}
public ICommand saveUser { get; private set; }
private void _saveUser()
{
this._pkServiceClient.updateUser(_selectedUser);
}
}
}
I believe it is possible to tie RelayCommand(saveUser, someCommand) and have someCommand return a value that I can bind to in my XAML to control the enabled state of my button.
I cannot seem to find a way using the Entity Generated class to handle this. any assistance would be wonderful.