我有雇主名单,这些雇主绑定数据并填写特殊表格。当我进入表单时,我会清楚地显示每个文本框。我填写所有这些并保存新雇主列表。但是,如果我尝试添加新的雇主,我会在文本框中添加以前的文本。绑定到表单中文本框的变量都是null。 有没有办法解决问题而不使用像这样的解决方案:Textbox.text = null;? 我在我的应用程序中使用MVVM模式。我还使用catel片段来定义视图模型和属性。页面的ViewModel代码包含雇主属性:
public EmployerModifyViewModel(TransferParameter parameter, IEmployersListManage employersListManager)
{
//in "parameter" I pass values fo Current employer (it can be empty
//if we need to add new object to list or it can be some employer from list)
_employersListManager = employersListManager;
SaveEmployerCommand = new Command(OnSaveEmployerCommandExecute);
CanselSavingCommand = new Command(OnCanselSavingCommandExecute);
if (parameter.Value is EmployerClass)
{
CurrentEmployer = parameter.Value as EmployerClass;
}
}
public EmployerClass CurrentEmployer
{
get { return GetValue<EmployerClass>(CurrentEmployerProperty); }
private set { SetValue(CurrentEmployerProperty, value); }
}
/// <summary>
/// Register the CurrentEmployerBase property so it is known in the class.
/// </summary>
public static readonly PropertyData CurrentEmployerProperty = RegisterProperty("CurrentEmployer", typeof(EmployerClass), new EmployerClass());
在xaml中有绑定属性的示例:
<ContentControl Content="{Binding CurrentEmployer, Mode=TwoWay}">
<ContentCntrol.Recources>
<DataTemplate DataType="{x:Type employer:EmployerClass}">
...
<TextBox Grid.Column="1"
x:Name="EmpName"
Width="300"
Height="30"
FontSize="14"
Text="{Binding Name, Mode=TwoWay}" //Property "Name" of CurrentEmployer
HorizontalAlignment="Left"
Margin="20,20,0,0"/>
答案 0 :(得分:0)
我认为您应该使用以下代码添加新员工。每次单击按钮时,文本框都会变空。
txtbox_1.Text = String.Empty;
txtbox_2.Text = String.Empty;
.............
答案 1 :(得分:0)
谢谢大家,问题解决了。我从xaml中删除了ContentControl和DataTemplate,我制作了像"{Binding CurrentEmployer.Name, Mode=TwoWay}"
这样的绑定。