我是WPF和MVVM的新手,在更改数据库中的项目时ListView不会更新。有没有人有他们如何完成这个的例子?
这是我的Listview:
<ListView DataContext="{Binding Source={StaticResource changpassVM}, UpdateSourceTrigger=Default}" BorderBrush="Black" Grid.Row="1" Name="lstUser" ItemsSource="{Binding TableFromMySqlEdit,UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path= SelectChangPass,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
IsSynchronizedWithCurrentItem = "True"
>
<ListView.View >
<GridView>
<GridViewColumn local:GridViewColumnVisibilityManager.IsVisible="False" Header="Id" HeaderContainerStyle="{StaticResource GridViewHeaderStyle }" DisplayMemberBinding="{Binding Id}" />
<GridViewColumn HeaderContainerStyle="{StaticResource GridViewHeaderStyle }" DisplayMemberBinding="{Binding Name}" Width="Auto" Header="Tên Người Dùng" />
<GridViewColumn HeaderContainerStyle="{StaticResource GridViewHeaderStyle }" DisplayMemberBinding="{Binding Pass,UpdateSourceTrigger=PropertyChanged}" Width="Auto" Header="Mật Khẩu" />
<GridViewColumn HeaderContainerStyle="{StaticResource GridViewHeaderStyle }" DisplayMemberBinding="{Binding Address}" Width="Auto" Header="Địa Chỉ" />
<GridViewColumn HeaderContainerStyle="{StaticResource GridViewHeaderStyle }" DisplayMemberBinding="{Binding Email}" Header="Email" Width="Auto"/>
<GridViewColumn HeaderContainerStyle="{StaticResource GridViewHeaderStyle}" DisplayMemberBinding="{Binding Createdate}" Header="Ngày Tạo" Width="Auto"/>
</GridView>
</ListView.View>
</ListView>
这是我的viewmodel:
public class changPassViewModel : INotifyPropertyChanged
{
#region property
// private DataTable _TableFromMySql;
private ObservableCollection<User> _TableFromMySql;
private readonly UserRepository ur;
private readonly changPassUser chuser;
private ICommand _editchangUserCmd;
#endregion end property
#region intialiti
public changPassViewModel()
{
ur = new UserRepository();
//_TableFromMySql = new List<changPassUser>
// {
// new changPassUser {ID=1, Name ="Pro1", Pass="10"},
// new changPassUser{ID=2, Name="BAse2", Pass="12"}
// };
chuser = new changPassUser();
// _TableFromMySql = ur.get_user_DataTable();
_TableFromMySql = ur.getData();
//this.DataContext =
_editchangUserCmd = new RelayCommand(Edit, CanEdit);
}
#endregion intia
#region
public int Id
{
get
{
return chuser.user_Id ;
}
set
{
chuser.user_Id = value;
OnPropertyChanged("Id");
}
}
public string Name
{
get
{
return chuser.user_Name;
}
set
{
chuser.user_Name = value;
OnPropertyChanged("Name");
}
}
public string Pass
{
get
{
return chuser.user_Pass;
}
set
{
chuser.user_Pass = value;
OnPropertyChanged("Pass");
}
}
public string Email
{
get
{
return chuser.user_Email;
}
set
{
chuser.user_Email = value;
OnPropertyChanged("Email");
}
}
public string Address
{
get
{
return chuser.user_Address;
}
set
{
chuser.user_Address = value;
OnPropertyChanged("Address");
}
}
#endregion
#region ListView
public ObservableCollection<User> TableFromMySqlEdit
{
get
{
return _TableFromMySql;
}
set
{
_TableFromMySql = value;
// OnPropertyChanged("TableFromMySqlEdit");
}
}
//public IList<changPassUser> TableFromMySql
//{
// get
// {
// return _TableFromMySql;
// }
// set
// {
// _TableFromMySql = value;
// }
//}
#endregion end ListView
// private ICommand mUpdater;
public ICommand EditPassuser
{
get
{
//if (_editchangUserCmd == null){
// mUpdater = new Updater();
return _editchangUserCmd;
// return mUpdater;
}
set
{
_editchangUserCmd = value;
}
}
public bool CanEdit(object obj)
{
if (Name != string.Empty && Pass != string.Empty && Pass != null)
return true;
return false;
}
public void Edit(object obj)
{
string a = "";
DateTime dt_edit = DateTime.Now;
//int Index;
// Index = UserRepository.GetIndex();
for (int i = 0; i < TableFromMySqlEdit.Count; i++)
if (TableFromMySqlEdit[i].Id == Id)
{
a = _TableFromMySql[i].Pass;
}
var user = new User
{
Id = Id,
// Name = Name,
Pass = Pass,
// Email = Email,
// Address = Address,
Modifieddate =dt_edit.ToString(),
// Status = 0
};
if (ur.Edit(user))
{
// TableFromMySqlEdit. (user);
// GetIndex(Id);
// _TableFromMySql[user.Id] = user.Pass;
// TableFromMySqlEdit = _TableFromMySql;
// Pass = user.Pass;
for (int i = 0; i < TableFromMySqlEdit.Count; i++)
if (TableFromMySqlEdit[i].Id == Id)
{
TableFromMySqlEdit[i].Pass = Pass;
}
ResetUser();
MessageBox.Show("Thêm người dùng thành công !");
}
else
MessageBox.Show("Thêm thất bại !");
}
/* private class Updater : ICommand
{
#region ICommand Members
private changPassUser changUser;
public Updater()
{
changUser = new changPassUser();
}
public bool CanExecute(object parameter)
{
// if (changUser.Name != string.Empty && changUser.Name != null)// && us.PASS != string.Empty)
// if (TableFromMySql.Count > 0)
return true;
// return false;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MessageBox.Show("Bạn có chắc muốn đóng?");
}
#endregion
}
*/
public User SelectChangPass
{
set
{
Id = value.Id;
Name = value.Name;
Address = value.Address;
Email = value.Email;
// CreateDate = value.user_;
// ModifiedDate = value.Modifieddate;
// Status = value.Status;
Pass = value.Pass;
}
}
private int GetIndex(int Id)
{
for (int i = 0; i < TableFromMySqlEdit.Count; i++)
if (TableFromMySqlEdit[i].Id == Id)
{
TableFromMySqlEdit[i].Pass = Pass;
}
return -1;
}
private void ResetUser()
{
// Id = 0;
Name = string.Empty;
Pass = string.Empty;
Address = string.Empty;
Email = string.Empty;
// MobileNumber = 0;
}
#region INotifyPropertyChanged Members
/// <summary>
/// Event to which the view's controls will subscribe.
/// This will enable them to refresh themselves when the binded property changes provided you fire this event.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// When property is changed call this method to fire the PropertyChanged Event
/// </summary>
/// <param name="propertyName"></param>
public void OnPropertyChanged(string propertyName)
{
//Fire the PropertyChanged event in case somebody subscribed to it
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
这是我的模特:
public int Id { get; set; }
public string Name { get; set; }
public string Pass { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Createdate { get; set; }
public string Modifieddate { get; set; }
public int Status { get; set; }