I am trying to implement my first MVVM application. I could bind the data in a datagrid, but the changes I make in the elements do not trigger the RaisePropertyChanged method of the model.
This is My ViewModel
public class UsersViewModel : BaseViewModel { private static TOPEntities _context; private ObservableCollection<UserModel> _usersCollection; public UsersViewModel() { _usersCollection = new ObservableCollection<UserModel>(GetAllUsers()); } public ObservableCollection<UserModel> UsersCollection { get { return _usersCollection; } set { if (_usersCollection != value) { _usersCollection = value; RaisePropertyChanged(() => UsersCollection); } } } public static List<UserModel> GetAllUsers() { using (_context = new TOPEntities()) { return _context.Users.Select (user => new UserModel { Id_User = user.Id_User, Name = user.Name, Username = user.Username, Language = user.Language, Password = user.Password, Profile = user.Profile }).ToList(); } }
The model implements the NotificationObject class, which provides INotifyPropertyChanged
public class UserModel : NotificationObject { #region Construction /// Constructs the default instance of a UserModel public UserModel() { } #endregion #region Model Attributes private int _id_User; private string _username; private string _password; private string _profile; private string _name; private string _language; #endregion #region Properties public int Id_User { get { return _id_User; } set { if (_id_User != value) { _id_User = value; RaisePropertyChanged(() => Id_User); } } } public string Username { get { return _username; } set { if (_username != value) { _username = value; RaisePropertyChanged(() => Id_User); } } } public string Password { get { return _password; } set { if (_password != value) { _password = value; RaisePropertyChanged(() => Id_User); } } } public string Profile { get { return _profile; } set { if (_profile != value) { _profile = value; RaisePropertyChanged(() => Id_User); } } } public string Name { get { return _name; } set { if (_name != value) { _name = value; RaisePropertyChanged(() => Name); } } } public string Language { get { return _language; } set { if (_language != value) { _language = value; RaisePropertyChanged(() => Language); } } } #endregion }
}
And finally, View:
<Window x:Class="TOP2.Views.UsersView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:viewModels="clr-namespace:TOP2.ViewModels" xmlns:local="TOP2" Title="Sample App" WindowStartupLocation="CenterScreen" Height="459" Width="795"> <Window.Resources> <viewModels:UsersViewModel x:Key="Windows1ViewModel" /> </Window.Resources> <Grid DataContext="{StaticResource Windows1ViewModel}"> <DataGrid ItemsSource="{Binding UsersCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="81,51,0,0" VerticalAlignment="Top" Height="332" Width="622"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <DataGridTextColumn Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> </DataGrid.Columns> </DataGrid> </Grid>
What am I forgetting or doing wrong?
Thanks in advance!
Oscar
Oscar Mateu
source share