当页面加载时,文本框不会自行清除
问题描述:
我有雇主列表,它们绑定到数据并从特殊形式填充。当我去表格时,我已经清楚每个文本框。我填写所有这些内容,并保存新雇主列表。但是,如果我尝试添加新雇主,我会在表单中包含以前文本的文本框。在窗体中绑定到文本框的变量都是空的。 有没有办法解决问题,而不使用这样的解决方案:Textbox.text = null ;? 我在我的应用程序中使用MVVM模式。我也使用catel片段来定义视图模型和属性。没有与用人单位属性页的视图模型的代码:当页面加载时,文本框不会自行清除
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"/>
答
谢谢大家,问题解决了。我从xaml中删除了ContentControl和DataTemplate,并且我制作了这样的绑定,如"{Binding CurrentEmployer.Name, Mode=TwoWay}"
。
答
我觉得你应该使用下面的代码,其中u添加新员工。每次点击按钮时,文本框都会变空。
txtbox_1.Text = String.Empty;
txtbox_2.Text = String.Empty;
.............
显示绑定以及如何在模型中定义属性。如果您有TwoWay绑定,您是否正确使用INotifyPropertyChanged? –
这些在模板中?可能是被重用的模板! – Muds
好的,我解决了我的问题并添加了更多细节。感谢您的关注 – Max