聲明一個名爲IsButtonEnable爲布爾屬性:
private bool isButtonEnable;
public bool IsButtonEnable
{
get
{
return isButtonEnable;
}
set
{
isButtonEnable = value;
OnPropertyChanged("IsButtonEnable");
}
}
綁定一個按鈕,這個屬性爲:
<Button Content="Save Data" IsEnable="{Binding IsButtonEnable, UpdateSourceTrigger=PropertyChanged}"></Button>
現在,在您的視圖模型綁定ObservableCollectionChanged事件爲:
public MyViewModel()
{
MyObsCollectionProp = new ObservableCollection<YourModel>();
MyObsCollectionProp += MyObsCollectionProp_Changed;
}
void MyObsCollectionProp_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
// Handle here
IsButtonEnable = MyObsCollectionProp.All(record=>record.IsValid)
}
回報作爲模型中的一個屬性? – Aybe
@Aybe,是的,但在viewModel,而不是模型,並將其綁定到xaml的IsEnabled屬性 –