【发布时间】:2016-12-10 22:55:47
【问题描述】:
这是我的观点:
<StackPanel Orientation="Horizontal" VerticalAlignment="Top">
<Label>Customer name:</Label>
<TextBox Text="{Binding Customer.Name, UpdateSourceTrigger=PropertyChanged}" Width="136"/>
<Button x:Name="UpdateClick">Update</Button>
</StackPanel>
这是我的视图模型:
private Customer customer;
public Customer Customer
{
get { return customer; }
set { customer = value; NotifyOfPropertyChange(() => Customer); }
}
public bool CanUpdateClick
{
get
{
if (string.IsNullOrEmpty(customer.Name))
{
return false;
}
return true;
}
}
public void UpdateClick()
{
//...
}
这是我的模型:
private string name;
public string Name
{
get { return name; }
set { name = value; NotifyOfPropertyChange(() => Name); }
}
所以我有 UpdateClick 方法,它工作得很好。我也有 CanUpdateClick 属性,但它不起作用,我不知道为什么?当文本框为空时,应禁用 UI 上的按钮。请帮忙!
【问题讨论】:
-
那是因为 notify 属性改变只是通知
Customer.Name改变了而不是CanUpdateClick。 Name 是客户类中的一个属性,因此必须利用客户的 propertychanged 才能获得该通知。
标签: c# wpf mvvm caliburn.micro