我正在研究WPF中的示例ViewModel,我目前正在将视图中的3个文本框绑定到Id
,FirstName
和LastName
。然后,我在xaml.cs文件中有一个按钮单击事件,该事件执行一个更新示例数据库表的方法。我试图围绕MVVM和WPF。我想取消按钮单击事件并绑定到Command。我正在使用Linq to Sql进行数据库连接。
当我调试代码时,一切都返回null。我在正确的道路上吗?
谢谢!
这是VM的一部分(我不想发布所有代码。)
public class People
{
public Guid Id {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
}
private People mPeople = null;
private Guid mId = Guid.empty;
public People people
{
get {return mPeople;}
set {mPeople = value; OnPropertyChanged("people");}
}
public overide void Refresh()
{
Using (DatabaseDataContext dc = new DatabaseDataContext())
{
mId = CurrentUser.Identity;
var query = from person in dc.PeopleTables
where person.PId = mId
select new People()
{
Id = person.PId,
FirstName = person.FirstName,
LastName = person.LastName
};
mPeople = query.First();
}
}
在VM中,我正在尝试使用命令
private RelayCommand mUserUpdate = null;
public ICommand UserUpdate
{
get
{
if(mUserUpdate == null) mUserUpdate = new RelayCommand(p => UpdateUser(p));
return mUserUpdate;
}
}
private void UpdateUser(object parameter)
{
People pe = parameter as People;
UpdateUser(pe.Id, pe.FirstName, pe.LastName);
}
public static void UpdateUser(Guid Id, string FirstName, string LastName)
{
DatabaseDataContext DC = new DatabaseDatacontext();
var User = (from c in DC.GetTable<PeopleTable>()
where c.PId = mId
select c).SingleOrDefault();
if (User == null
{
//Do my inserts here
}
else
{
try
{
User.Id = Id;
User.FirstName = FirstName;
User.LastName = LastName;
DC.SubmitChanges();
}
catch
{
throw ex;
}
}
}
这是我正在使用的点击事件
private void btnSave_Click(object sender, RoutedEventArgs e)
{
UserViewModel.UpdateUser(txtId.Text, txtFirstName.Text, txtLastName.Text);
}
是的,我拿出了点击事件并将其替换为
<Button Command="{Binding Path=UserUpdate}"/>
我正在绑定像这样的文本框
<TextBox Width="250" Text="{Binding Path=Id}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<Label Content="First Name:" Margin="0,0,4,0"/>
<TextBox Width="250" Text="{Binding Path=FirstName}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0,5,0,0">
<Label Content="Last Name:" Margin="35,0,4,0"/>
<TextBox Width="250" Text="{Binding Path=LastName}"/>
答案 0 :(得分:2)
命令旨在replace
按钮的Click
事件处理程序。
您应该删除事件处理程序并执行
<Button Command="{Binding UserUpdate}"/>
答案 1 :(得分:2)
您可能需要设置命令参数:
<Button Command="{Binding UserUpdate}" CommandParameter="{Binding SelectedUser}"/>
如果“SelectedUser”来自您的VM而不是当前的UI,则您不需要该参数。只需获取要在VM中修改的用户intance的引用。
答案 2 :(得分:2)
因为你的代码没有用,因为你期望传递一个类型为Person的参数,这是不可能的命令(我认为)。
您的视图模型需要如下所示:
public class PersonViewModel : *INotifyPropertyChanged base class here*
{
public PersonViewModel()
{
UpdatePersonCommand = new RelayCommand(UpdateUser);
}
public Guid Id {get{ return _id;} { set _id = Id; RaisePropertyChanged("Id"); }
public string FirstName { ... same }
public string LastName { ... same }
public ICommand UpdatePersonCommand {get; private set;}
private void UpdateUser()
{
UpdateUser(Id, FirstName, LastName);
}
private void UpdateUser(Guid Id, string FirstName, string LastName)
{
DatabaseDataContext DC = new DatabaseDatacontext();
...
}
}
答案 3 :(得分:1)
在UpdateUser方法
中尝试此操作Private void UpdateUser(object obj)
{
UpdateUser(Id, FirstName, LastName);
}