I’m using a MVVM pattern with Prism
I’ve got a list of Elements in an ObservableCollection, in my Model. In my View, i binded the ObservableCollection to a Listview, then i trye to display 1 button that is binded to a DelegateCommand called DoSomethingCommand. That means that each line of my Listview have a button to call DoSomethingCommand, with a CommandParameter binded to the ID of the element.
Constraint: DoSomethingCommand will change the status field of my element to “done”. When an element is “done” i want the button to call DoSomethingCommand to be disabled.
So logically, all i have to do is have a canExecuteDoSomethingCommand delegate when i implement the command. But, the problem is when and how can i raise DoSomethingCommand.RaiseCanExecuteChanged?
BTW Element is part of a third party dll that i can’t modify it, but it already implements INotifyPropertyChanged.
<ListView BorderThickness="0" Width="Auto"
ItemsSource="{Binding ElementList}"
>
<ListView.View>
<GridView>
<GridViewColumn Header="Actions">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button Command="{Binding DataContext.DoSomethingCommand, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" CommandParameter="{Binding ID}" >Accept</Button>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
Implemting the command
DoSomethingCommand = new DelegateCommand<string>(executeDoSomethingCommand, canExecuteDoSomethingCommand);
If you are using Prism’s EventAggregator, and I assume that you are since you’re using Prism, then you can subscribe to an event and call RaiseCanExecuteChanged() on your DelegateCommand.
Something like this: