【问题标题】:How to clear textboxes after update method更新方法后如何清除文本框
【发布时间】:2013-06-28 11:33:09
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf wcf mvvm


    【解决方案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;
        }
    }
    

    【讨论】:

    • 非常感谢。我为此浪费了几天时间,这只是一行代码......
    【解决方案2】:

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

    User=new ClSUser();
    

    SelectedUser 以此类推。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 2018-04-06
      • 2011-04-14
      相关资源
      最近更新 更多