如何在更新方法后清除文本框
本文关键字:清除 文本 新方法 更新 | 更新日期: 2023-09-27 18:20:19
我有3个文本框、一个复选框、列表视图和3个按钮。现在,文本框和复选框已绑定到ManageUsersViewModel中的属性"User"。ListView已将其选定项绑定到属性"SelectedUser"。按钮命名为"添加"、"编辑"、"保存"。每个按钮都绑定到自己的命令。"添加"按钮的命令运行良好。当我点击它时,我从WCF服务中调用一个方法,该方法将新用户插入数据库。当我在列表视图中选择某一行,然后单击"编辑"复选框按钮,文本框就会填充我选择的值。现在我可以通过点击按钮"保存"来更改值并保存更改。问题是,在这个过程之后,我不能再添加新用户。当我尝试添加新用户时,我在文本框中输入的值也会更改我之前更改的值。基本上,我不能再做添加功能,在更新功能之后,直到我重新启动窗口。此外,我希望我的文本框和复选框在添加或更新功能后被清除。这是我的代码:
管理用户视图模型
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;
}
}
}
管理用户.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;
}
}
添加和更新过程完成后,将User和SelectedUser属性设置为新实例。例如如果User是ClSUser类的对象,则
User=new ClSUser();
对于SelectedUser,依此类推。