页面加载时文本框不会自行清除
Textboxes don't clear themselves when page loaded
我有绑定数据并从特殊表格填写的雇主名单。当我去表格时,我会清除每个文本框。我填写所有这些并将新雇主保存到列表中。但是,如果我尝试添加新的雇主,我会在表格中显示带有先前文本的文本框。并且在表单中绑定到文本框的变量都是空的。
有没有办法解决问题而不使用这样的解决方案: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"/>
我认为您应该在添加新员工的地方使用以下代码。每次单击按钮时,文本框都会变空。
txtbox_1.Text = String.Empty;
txtbox_2.Text = String.Empty;
.............
谢谢大家,问题解决了。我从 xaml 中删除了 ContentControl 和 DataTemplate 并且我做了这样的绑定 "{Binding CurrentEmployer.Name, Mode=TwoWay}"
.
我有绑定数据并从特殊表格填写的雇主名单。当我去表格时,我会清除每个文本框。我填写所有这些并将新雇主保存到列表中。但是,如果我尝试添加新的雇主,我会在表格中显示带有先前文本的文本框。并且在表单中绑定到文本框的变量都是空的。 有没有办法解决问题而不使用这样的解决方案: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"/>
我认为您应该在添加新员工的地方使用以下代码。每次单击按钮时,文本框都会变空。
txtbox_1.Text = String.Empty;
txtbox_2.Text = String.Empty;
.............
谢谢大家,问题解决了。我从 xaml 中删除了 ContentControl 和 DataTemplate 并且我做了这样的绑定 "{Binding CurrentEmployer.Name, Mode=TwoWay}"
.