![](/img/trans.png)
[英]Need to clear values of textboxes placed in update panel after button click
[英]How to clear textboxes after update method
我有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}">
更改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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.