在WPF中使用iccommand——命令返回null

本文关键字:命令 返回 null iccommand WPF | 更新日期: 2023-09-27 18:09:28

我正在WPF中研究一个示例ViewModel,我目前将视图中的3个文本框绑定到Id, FirstNameLastName。然后,在xaml.cs文件中有一个按钮单击事件,该事件执行更新示例数据库表的方法。我正试图把我的头围绕MVVM和WPF。我想取消按钮单击事件并绑定到命令。我正在使用Linq到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();
    }
}

在虚拟机中,这是我尝试使用命令

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);
}

是的,我去掉了click事件,代之以

<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}"/>

在WPF中使用iccommand——命令返回null

命令用于replace按钮的Click事件处理程序。

您应该删除事件处理程序并执行

<Button Command="{Binding UserUpdate}"/>

您可能需要设置命令参数:

<Button Command="{Binding UserUpdate}" CommandParameter="{Binding SelectedUser}"/>

如果"SelectedUser"来自你的VM而不是当前的UI,你不需要这个参数。只需获取要在VM中修改的用户实例的引用。

因为你的代码不工作,因为你期望一个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();
        ...
    }
}

在UpdateUser方法中试试

Private void UpdateUser(object obj)
{
    UpdateUser(Id, FirstName, LastName);
}