我有僱主列表,它們綁定到數據並從特殊形式填充。當我去表格時,我已經清楚每個文本框。我填寫所有這些內容,並保存新僱主列表。但是,如果我嘗試添加新僱主,我會在表單中包含以前文本的文本框。在窗體中綁定到文本框的變量都是空的。 有沒有辦法解決問題,而不使用這樣的解決方案: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"/>
顯示綁定以及如何在模型中定義屬性。如果您有TwoWay綁定,您是否正確使用INotifyPropertyChanged? –
這些在模板中?可能是被重用的模板! – Muds
好的,我解決了我的問題並添加了更多細節。感謝您的關注 – Max