【发布时间】:2015-09-01 13:32:19
【问题描述】:
我的问题是,当我更改绑定属性时,我的 CanSave 方法没有被调用。
查看
视图包含一些Labels 和TextBoxes...没什么特别的...
文本框绑定到对象的属性。
<Label Target="{Binding ElementName=txtTitle}" Grid.Column="0" Grid.Row="0" Content="Titel" Foreground="White" />
<TextBox Name="txtTitle" Grid.Column="1" Grid.Row="0" Text="{Binding NewBook.Title}" />
<Label Target="{Binding ElementName=txtAuthor}" Grid.Column="0" Grid.Row="1" Content="Autor" Foreground="White" />
<TextBox Name="txtAuthor" Grid.Column="1" Grid.Row="1" Text="{Binding NewBook.Author}"/>
<Label Target="{Binding ElementName=txtIsbn}" Grid.Column="0" Grid.Row="2" Content="ISBN" Foreground="White" />
<TextBox Name="txtIsbn" Grid.Column="1" Grid.Row="2" Text="{Binding NewBook.Isbn}" />
<Label Target="{Binding ElementName=txtPrice}" Grid.Column="0" Grid.Row="3" Content="Preis" Foreground="White" />
<TextBox Name="txtPrice" Grid.Column="1" Grid.Row="3" Text="{Binding NewBook.Price}"/>
<Button Grid.Column="1" Grid.Row="4" Content="Speichern" Name="SaveBook" />
视图模型
ViewModel 定义了属性NewBook 和方法CanSaveBook 和SaveBook
public Book NewBook
{
get { return this.newBook; }
set
{
this.newBook = value;
this.NotifyOfPropertyChange(() => this.NewBook);
this.NotifyOfPropertyChange(() => this.CanSaveBook);
}
}
public bool CanSaveBook
{
get
{
return !string.IsNullOrEmpty(this.NewBook.Title)
&& !string.IsNullOrEmpty(this.NewBook.Author)
&& !string.IsNullOrEmpty(this.NewBook.Isbn)
&& this.NewBook.Price != 0;
}
}
public void SaveBook()
{
try
{
this.xmlOperator.AddBook(ConfigurationManager.AppSettings.Get("BookXmlPath"), this.NewBook);
}
catch (Exception ex)
{
throw ex;
}
this.NewBook = new Book();
}
我想要的是,Save-Button 仅在CanSaveBook 返回true 时可用,但在用户在文本框中输入信息后检查CanSaveBook-方法...我错过了什么?
【问题讨论】:
标签: c# .net wpf mvvm caliburn.micro