我有我的 Model
实现 INotifyPropertyChanged
的类.
我的 View 有 5 个 TextBox
, 2 Button
和 ListView
显示网格。
在我的 ViewModel
我之前将默认值添加到 ObservableCollection
我的Model
类并将其显示到 ListView
.
使用 ICommand
完成按钮实现。和 RelayCommand
.
现在我想将数据添加到 ObservableCollection
来自 UI 中的用户 TextBox
.我怎样才能做到这一点?用户界面 TextBox
具有 Model
属性的 hasbindings类(class)。
我的 View
<ListView Name="UserGrid" Grid.Row="1" Margin="4,178,12,13" ItemsSource="{Binding UserDatas}" >
<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,7,0,0" Name="txtUserId" VerticalAlignment="Top" Width="178" Text="{Binding UserId}" />
<TextBox Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="80,35,0,0" Name="txtFirstName" VerticalAlignment="Top" Width="178" Text="{Binding FirstName}" />
像这样有 5 个文本框
Model
类(class):public class User : INotifyPropertyChanged
{
private int userId;
private string firstName;
public int UserId
{
get
{
return userId;
}
set
{
userId = value;
RaisePropertyChanged("UserId");
}
}
}
View 模型:
public class UsersViewModel:INotifyPropertyChanged
{
private ObservableCollection<User> userDatas;
public ObservableCollection<User> UserDatas
{
get
{
if (userDatas == null)
{
userDatas = new ObservableCollection<User>();
}
return userDatas;
}
set
{
userDatas = value;
RaisePropertyChanged("UserDatas");
}
}
}
private CommandBase _LoadCommand;
public ICommand LoadCommand
{
get
{
if (this._LoadCommand == null)
this._LoadCommand = new CommandBase(LoadData);
return this._LoadCommand;
}
}
private void LoadData(object obj)
{
//What need to be done here to access the textboxes of UI which are binded to User.cs class.
User newUser = new User();
UserDatas.Add(newUser);
}
现在我需要在 LoadData 方法中编写从 UI 的文本框获取输入并将其存储在我的
ObservableCollection
中的内容
最佳答案
您可以做几件事。最明显的是有一个“添加新”命令,它创建一个新的空对象并将其存储在 CurrentUser
中。或 SelectedUser
属性(property)。
此属性绑定(bind)到模板(或表单)的上下文。您将有 3 个命令(添加新用户、保存用户、取消以取消添加新用户创建)。
例如
public class UsersViewModel : INotifyPropertyChanged
{
public UsersViewModel()
{
UserDatas = new ObservableCollection<User>();
AddNewUserCommand = new RelayCommand(AddNewUser, param => !this.IsNewUser);
SaveUserCommand = new RelayCommand(SaveUser);
CancelNewUserCommand = new RelayCommand(CancelNewUser, param => this.IsNewUser);
}
private ObservableCollection<User> userDatas;
public ObservableCollection<User> UserDatas
{
get { return userDatas; }
set
{
userDatas = value;
RaisePropertyChanged("UserDatas");
}
}
private User selectedUser;
public User SelectedUser
{
get { return selectedUser; }
set
{
selectedUser = value;
RaisePropertyChanged("SelectedUser");
RaisePropertyChanged("IsNewUser");
}
}
public bool IsNewUser
{
get
{
if(SelectedUser==null)
return false;
return SelectedUser.UserId == 0;
}
}
public ICommand AddNewUserCommand { get; private set; }
public ICommand CancelNewUserCommand { get; private set; }
public ICommand SaveUserCommand { get; private set; }
private void AddNewUser()
{
SelectedUser = new User();
}
private void SaveUser()
{
// Just in case of concurency
var newUser = SelectedUser;
if(newUser == null)
{
return;
}
var isNewUser = newUser.UserId == 0;
// Persist it to the database
this.userRepository.Add(newUser);
this.userRepository.SaveChanges();
// If all worked well, add it to the observable collection
if(isNewUser)
{
// Only add if new, otherwise it should be already in the collection
UserDatas.Add(newUser)
}
}
}
但同样,非常不鼓励直接在模型上工作并将其绑定(bind)到 View 。您还应该为您的用户创建一个 ViewModel 并将验证(在
IDataErrorInfo
上实现 UserViewModel
接口(interface))并处理状态,例如跟踪 UserViewModel
是脏的(即数据已更改)。所有这些都是表示问题而不是业务逻辑,因此它们属于 ViewModel 而不是 Model 本身。
关于c# - 使用 MVVM WPF 将数据保存到 Collection 和 Display,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35218551/