Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新方法后如何清除文本框_C#_Wpf_Wcf_Mvvm - Fatal编程技术网

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

C# 更新方法后如何清除文本框,c#,wpf,wcf,mvvm,C#,Wpf,Wcf,Mvvm,我有3个文本框,一个复选框,列表视图和3个按钮。现在,Xtboxs和复选框绑定到ManageUsersViewModel中的属性“User”。ListView将其所选项目绑定到属性“SelectedUser”。按钮名为“添加”、“编辑”、“保存”。每个按钮都绑定到自己的命令。“添加”按钮的命令工作正常。当我点击它时,我从WCF服务调用一个方法,该方法将新用户插入数据库。当我在listview中选择某一行,并单击按钮“编辑”复选框时,文本框将填充我选择的值。现在我可以通过点击“保存”按钮来更改值和

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

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;
        }
    }
}
classmanageusersviewmodel:ViewModelBase
{
#区域构造函数
私有服务引用1.tblUser用户;
公共服务引用1.tblUser用户
{
得到
{
返回用户;
}
设置
{
用户=价值;
OnPropertyChanged(“用户”);
}
}
私人服务参考1.t luser selectedUser;
公共服务参考1.tblUser SelectedUser
{
得到
{
返回所选的用户;
}
设置
{
selectedUser=值;
OnPropertyChanged(“SelectedUser”);
}
}
私有可观察收集用户;
公共ObservableCollection用户//属性“服务”
{
得到
{
返回用户;
}
设置
{
用户=价值;
OnPropertyChanged(“用户”);
}
}
public ManageUsersViewModel()
{
}//Konstruktor
#端区
公共ICommand_addUser;
公共ICommand addUser
{
得到
{
如果(_addUser==null)
{
_addUser=newdelegateCommand(delegate()
{
尝试
{                            
Service1Client wcf=新Service1Client();
wcf.AddUser(用户);
用户。添加(用户);
wcf.Close();
}
抓住
{
Trace.WriteLine(“工作…”,“MyApp”);
}
});
}
返回_addUser;
}
}
公共ICommand_b编辑程序;
公共ICommand btnEditUser
{
得到
{
如果(_btnEditUser==null)
{
_btnEditUser=新的DelegateCommand(delegate()
{
尝试
{
用户=所选用户;
}
抓住
{
Trace.WriteLine(“工作…”,“MyApp”);
}
});
}
返回编辑器;
}
}
公共ICommand_btnUpdateUser;
公共ICommand BtnUpdate用户
{
得到
{
if(_btnUpdateUser==null)
{
_btnUpdateUser=新的DelegateCommand(delegate()
{
尝试
{
Service1Client wcf=新Service1Client();
wcf.updateUser(选择的用户);
wcf.Close();
}
抓住
{
Trace.WriteLine(“工作…”,“MyApp”);
}
});
}
返回_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}">

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

添加和更新过程完成后,将用户和SelectedUser属性设置为新实例。 例如 若用户是ClSUser类的对象,则

User=new ClSUser();

选择的用户请继续。

非常感谢。我为此损失了几天,这只是一行代码。。。