我有一个问题,当我更改绑定属性时,我的
CanSave
方法没有被调用。
视图
视图包含一些 标签
和 文本框
... 没有什么特别的...
这些文本框绑定到对象的属性。
<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" />
视图模型
视图模型定义了属性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();
}
我想要的是,当用户在文本框中输入信息后才检查CanSaveBook
方法是否返回true
,并且只有在其返回true
时 Save-Button 可用……我缺少什么?