如何在更新方法后清除文本框

时间:2013-06-28 11:33:10

标签: c# wpf wcf mvvm

我有3个文本框,一个复选框,listview和3个按钮。现在,thextbox和一个复选框绑定到ManageUsersViewModel中的属性“User”。 ListView将其选定项绑定到属性“SelectedUser”。按钮被命名为“添加”,“编辑”,“保存”。每个按钮都绑定到它自己的命令。 “添加”按钮的命令工作正常。当我点击它时,我从WCF服务调用一个方法,该方法将新用户插入数据库。当我在列表视图中选择一行时,单击“编辑”按钮复选框,文本框中填充了我选择的值。现在我可以通过单击“保存”按钮来更改值并保存更改。问题是,我无法在此过程后再次添加新用户。当我尝试添加新用户时,我输入文本框的值也会更改我之前更改的值。 Bassicly我无法再次执行添加功能,更新功能后直到我重新启动窗口。此外,我想要我的文本框,并在添加或更新功能后清除复选框。这是我的代码:

ManageUsersViewModel

class ManageUsersViewModel : ViewModelBase
{
   #region Constructor

    private ServiceReference1.tblUser user;
    public ServiceReference1.tblUser User    
    {
        get
        {
            return user;
        }
        set
        {
            user = value;
            OnPropertyChanged("User");
        }
    }

    private ServiceReference1.tblUser selectedUser;
    public ServiceReference1.tblUser SelectedUser
    {
        get
        {
            return selectedUser;
        }
        set
        {
            selectedUser = value;
            OnPropertyChanged("SelectedUser");
        }
    }


    private ObservableCollection<ServiceReference1.tblUser> users;
    public ObservableCollection<ServiceReference1.tblUser> Users     // Property "Service"
    {
        get
        {
            return users;
        }
        set
        {
            users = value;
            OnPropertyChanged("Users");
        }
    }

    public ManageUsersViewModel()
    {
    }   // Konstruktor

    #endregion

    public ICommand _addUser;
    public ICommand addUser 
    {
        get
        {
            if (_addUser == null)
            {
                _addUser = new DelegateCommand(delegate()
                {
                    try
                    {                            
                        Service1Client wcf = new Service1Client();
                        wcf.AddUser(User);
                        Users.Add(User);
                        wcf.Close();
                    }
                    catch
                    {
                        Trace.WriteLine("working...", "MyApp");
                    }
                });
            }

            return _addUser;
        }
    }

 public ICommand _btnEditUser;
    public ICommand btnEditUser
    {
        get
        {
            if (_btnEditUser == null)
            {
                _btnEditUser = new DelegateCommand(delegate()
                {
                    try
                    {
                        User = SelectedUser;

                    }
                    catch
                    {
                        Trace.WriteLine("working...", "MyApp");
                    }
                });
            }

            return _btnEditUser;
        }
    }

    public ICommand _btnUpdateUser;
    public ICommand btnUpdateUser
    {
        get
        {
            if (_btnUpdateUser == null)
            {
                _btnUpdateUser = new DelegateCommand(delegate()
                {
                    try
                    {
                        Service1Client wcf = new Service1Client();
                        wcf.updateUser(SelectedUser);
                        wcf.Close();                           


                    }
                    catch
                    {
                        Trace.WriteLine("working...", "MyApp");
                    }
                });
            }

            return _btnUpdateUser;
        }
    }
}

ManageUsers.xaml

<TextBox Height="25" HorizontalAlignment="Left" Margin="124,12,0,0" Name="txtName" VerticalAlignment="Top" Width="156" BorderBrush="#89000000" FontFamily="Times New Roman" FontSize="14" TabIndex="0" Text="{Binding Path=User.Name}" />
    <TextBox Height="25" HorizontalAlignment="Left" Margin="124,43,0,0" Name="txtNewUsername" VerticalAlignment="Top" Width="156" BorderBrush="#89000000" FontFamily="Times New Roman" FontSize="14" TabIndex="1" Text="{Binding Path=User.Username}" />
    <TextBox Height="25" HorizontalAlignment="Left" Margin="124,74,0,0" Name="txtPassword" VerticalAlignment="Top" Width="156" BorderBrush="#89000000" FontFamily="Times New Roman" FontSize="14" TabIndex="2" Text="{Binding Path=User.Password}" />
    <Button Content="Dodaj" Height="23" HorizontalAlignment="Left" Margin="286,12,0,0" Name="btnAddUser" VerticalAlignment="Top" Width="73" BorderBrush="Black" FontFamily="Times New Roman" FontWeight="Bold" FontSize="15" IsDefault="True" Command="{Binding addUser}" />
    <Button Content="Izmeni" Height="23" HorizontalAlignment="Left" Margin="381,152,0,0" Name="btnEditUser" VerticalAlignment="Top" Width="73" BorderBrush="Black" FontFamily="Times New Roman" FontWeight="Bold" FontSize="15" Command="{Binding btnEditUser}" />
    <Button BorderBrush="Black" Content="Sačuvaj" FontFamily="Times New Roman" FontSize="15" FontWeight="Bold" Height="23" HorizontalAlignment="Left" IsDefault="True" Margin="361,12,0,0" Name="btnUpdateUser" VerticalAlignment="Top" Width="73" Visibility="Visible" Command="{Binding btnUpdateUser}"/>
    <CheckBox Content="Administrator" Height="16" HorizontalAlignment="Left" Margin="124,110,0,0" Name="checkBox1" VerticalAlignment="Top" BorderBrush="#89000000" FontFamily="Times New Roman" FontWeight="Bold" FontSize="14" TabIndex="3" IsChecked="{Binding User.IsAdmin}" />
    <ListView SelectionMode="Single" Height="204" HorizontalAlignment="Left" Margin="7,152,0,0" Name="lvUsers" VerticalAlignment="Top" Width="368" FontFamily="Times New Roman" FontSize="14" ItemsSource="{Binding Path=Users}" FontWeight="Bold" Foreground="Black" BorderBrush="Black" SelectedItem="{Binding Path=SelectedUser}">

2 个答案:

答案 0 :(得分:1)

更改adduser命令以将用户设置为新用户:

这应该清除字段并允许创建新用户。

public ICommand addUser 
{
    get
    {
        if (_addUser == null)
        {
            _addUser = new DelegateCommand(delegate()
            {
                try
                {                            
                    Service1Client wcf = new Service1Client();
                    wcf.AddUser(User);
                    Users.Add(User);
                    wcf.Close();
                    this.User = new User();
                }
                catch
                {
                    Trace.WriteLine("working...", "MyApp");
                }
            });
        }

        return _addUser;
    }
}

答案 1 :(得分:1)

完成添加和更新过程后,将User和SelectedUser属性设置为新实例。 例如 如果User是ClSUser类的对象,那么

User=new ClSUser();

依旧为SelectedUser。