1

I am working on a sample ViewModel in WPF and I am currently binding 3 textboxes in the View to Id, FirstName, and LastName. I then have a button click event in the xaml.cs file that executes a method that updates the sample database table. I am trying to wrap my head around MVVM and WPF. I want to take away the button click event and bind to a Command instead. I am using Linq to Sql for the db connection.

When I am debugging the code everything is returning null. Am I on the right path?

Thanks!

Here is part of the VM (I didnt want to post all the code.)

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

In the VM here is what I am trying with the Commands

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

Here is my click event I was using

private void btnSave_Click(object sender, RoutedEventArgs e)
{
    UserViewModel.UpdateUser(txtId.Text, txtFirstName.Text, txtLastName.Text);
}

Yea, I took out the click event and replaced it with

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

I am binding the textboxes like this

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

4 回答 4

2

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

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

如果“SelectedUser”来自您的 VM 而不是当前 UI,则您不需要该参数。只需获取要在 VM 中修改的用户实例的引用即可。

于 2013-04-16T03:05:30.030 回答
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();
        ...
    }

}
于 2013-04-16T06:27:32.277 回答
2

命令用于按钮replaceClick事件处理程序。

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

<Button Command="{Binding UserUpdate}"/>
于 2013-04-16T02:36:41.747 回答
1

在 UpdateUser 方法中试试这个

Private void UpdateUser(object obj)
{
    UpdateUser(Id, FirstName, LastName);
}
于 2013-04-16T14:44:13.487 回答