我有一个包含多个文本框输入的窗口。 当我单击"编辑"时,它会加载到当前用户中,如果我进行更改然后决定取消,它不会在取消时删除更改。
如果我尝试再次编辑同一个人,我之前所做的更改仍在输入字段中。
这就是我调用编辑窗口的方式
<Button x:Uid="Button_EditCommand" Margin="0,0,2,0" Command="{Binding Path=EditProviderCommand}" Style="{StaticResource btnCustom}" MinWidth="75" Content="Save" Visibility="{Binding Path=IsEditing, Converter={StaticResource VisibilityConverter}}" />
然后调用该框,这是我的取消方法
CancelCommand = new RelayCommand(Cancel);
public ICommand CancelCommand { get; private set; }
private void Cancel()
{
Provider = null;
OnRequestClose();
}
取消方法与 Add 方法共享。 当我输入添加并单击取消时,它会清除所有更改。 如何让它清除已编辑的字段而不是将它们绑定到绑定?
文本框:
<TextBox x:Uid="TextBox_1" Grid.Column="1" Grid.Row="0" Tag="{Binding Path=FirstNameLabel, Source={StaticResource Clientization}}" Style="{StaticResource EditTextBox}" MaxLength="35"
Text="{Binding Path=Provider.FirstName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_2" Grid.Column="2" Grid.Row="0" Tag="Middle" MinWidth="75" Style="{StaticResource EditTextBox}" MaxLength="30"
Text="{Binding Path=Provider.MiddleName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_3" Grid.Column="1" Grid.Row="1" Tag="{Binding Path=LastNameLabel, Source={StaticResource Clientization}}" Style="{StaticResource EditTextBox}" MaxLength="60"
Text="{Binding Path=Provider.LastName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_4" Grid.Column="2" Grid.Row="1" Tag="Suffix" Style="{StaticResource EditTextBox}" MaxLength="20"
Text="{Binding Path=Provider.Suffix, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_5" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="2" Tag="List Name*" Style="{StaticResource EditTextBox}" MaxLength="160"
Text="{Binding Path=Provider.ListName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBox x:Uid="TextBox_6" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="3" Tag="NPI*" Style="{StaticResource EditTextBox}" MaxLength="80"
Visibility="{Binding Path=HideNpi, Source={StaticResource Clientization}, Converter={StaticResource TernaryConverter}, ConverterParameter='True ? Collapsed : Visible'}"
Text="{Binding Path=Provider.NPI, UpdateSourceTrigger=PropertyChanged}"/>
更新:
我的提供程序副本
public AddEditProviderDialogViewModel(IProviderRepository providerRepository, IMedListProvider medListProvider,
Provider provider, bool isEditing)
: this(providerRepository, medListProvider, provider)
{
_isEditing = isEditing;
Provider editProvider = new Provider()
{
FirstName = provider.FirstName,
LastName = provider.LastName,
Prefix = provider.Prefix,
Suffix = provider.Suffix,
ListName = provider.ListName,
NPI = provider.NPI,
OrgName = provider.OrgName,
Address1 = provider.Address1,
Address2 = provider.Address2,
City = provider.City,
State = provider.State,
Zip = provider.Zip,
EmailAddress = provider.EmailAddress,
Phone1Type = provider.Phone1Type,
Phone2Type = provider.Phone2Type,
Phone1 = provider.Phone1,
Phone2 = provider.Phone2
};
大概有了Add
,你只是对编辑后的模型什么都不做,下次你创建一个新模型。 我看不到任何东西会被清除 - 只是没有添加。
Cancel
不会撤消任何更改,您必须自己处理。 也许克隆当前项目并对其进行编辑。 接受后,您必须将更改复制回原始更改。
如果您需要任何特定帮助,我们需要查看更多代码。 也许是一个简单的例子。
我这样做的方法是:创建对象的副本。然后创建了一个撤消方法并将其绑定到取消。undo 方法将执行以下操作:editedobject = originalobject。当我回到家时,邮政编码生病了。
如果您有一个包装User
模型实例的UserViewModel
类,则可以使用以下模式实现取消编辑。基本上,在编辑时,您将模型复制到 _ originalModel
变量,然后选择是否在取消时恢复到原始模型。
public class UserViewModel : ViewModelBase
{
private User _originalModel;
private User _model;
public User Model
{
get { return _model; }
set
{
_model = value;
_originalModel = _model;
}
}
public DelegateCommand EditCommand { get; set; }
public DelegateCommand CancelCommand { get; set; }
public DelegateCommand SaveCommand { get; set; }
public UserViewModel()
{
EditCommand = new DelegateCommand(OnEditCommandExecuted);
CancelCommand = new DelegateCommand(OnCancelCommandExecuted);
SaveCommand = new DelegateCommand(SaveCommandExecuted);
}
private void SaveCommandExecuted(object obj)
{
_originalModel = _model;
}
private void OnCancelCommandExecuted(object obj)
{
_model = _originalModel;
OnPropertyChanged(null);
}
private void OnEditCommandExecuted(object obj)
{
_originalModel = _model;
}
public string FirstName
{
get { return Model.FirstName; }
set
{
if (value == Model.FirstName) return;
Model.FirstName = value;
OnPropertyChanged();
}
}
public string LastName
{
get { return Model.LastName; }
set
{
if (value == Model.LastName) return;
Model.LastName = value;
OnPropertyChanged();
}
}
}