I would like to enable a given wizard page when all preceding pages are valid. Here is my view model:
[Aggregatable]
[NotifyPropertyChanged]
[ContentProperty("Pages")]
public class Wizard
{
[Child, AggregateAllChanges]
public AdvisableCollection<Page> Pages { get; } = new AdvisableCollection<Page>();
}
Here is the Page
itself:
[Aggregatable]
[NotifyPropertyChanged]
public class Page : INotifyPropertyChanged
{
[Parent] Wizard Wizard { get; set; }
public string Name { get; set; }
public bool Valid { get; set; }
[SafeForDependencyAnalysis]
public bool Enabled
{
get
{
if(Depends.Guard)
Depends.On(Wizard.Pages);
return Wizard.Pages
.TakeWhile(p => p != this)
.All(p => p.Valid);
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
void OnPropertyChanged(string propertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
if (Wizard != null)
NotifyPropertyChangedServices.SignalPropertyChanged(Wizard, nameof(Wizard.Pages));
}
}
I was expecting PostSharp to notify about Enabled
property change when Wizard.Pages
changes. It does not work unfortunately – there is no updates to Enabled
properties. What is wrong about this code?
XAML to test:
<Window.DataContext>
<local:Wizard>
<local:Page Name="First"/>
<local:Page Name="Second"/>
<local:Page Name="Third"/>
<local:Page Name="Forth"/>
</local:Wizard>
</Window.DataContext>
<ListBox ItemsSource="{Binding Pages}">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type local:Page}">
<CheckBox Content="{Binding Name}" IsChecked="{Binding Valid}" IsEnabled="{Binding Enabled}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>