views:

808

answers:

2

I have a template for treeView item:

<HierarchicalDataTemplate x:Key="RatesTemplate">
    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=ID}"/>
                        <Button CommandParameter="{Binding Path=ID}" 
                                Command="{Binding ElementName=CalcEditView, Path=DataContext.Add}">Add</Button>                            
    </StackPanel>
</HierarchicalDataTemplate>

As a DataContext I have linq entity with ID not null field.

The problem is: if I use DelegateCommand 'Add' with CanExecutedMethod:

AddRate = new DelegateCommand<int?>(AddExecute,AddCanExecute);

its called only once and parameter is null (while textBlock shows proper ID value). CanExecute is called before ID property is called (checked with debugger). Seems like before binding to actual parameter wpf is invoking canExecute and forgets about it. Once binding finished and proper value loaded it doesn't call CanExecute again.

As a workaround I can use command with only execute delegate:

Add = new DelegateCommand<int?>(AddExecute);

AddExecute is invoked with correct ID value and is working perfectly. But I still want to use CanExecute functionality. Any ideas?

+1  A: 

You can try to use CommandManager.InvalidateRequerySuggested to force an update.

bitbonk
A: 

Didn't found the way to use commandManager. Just found another workaround: I used this code for button loaded event:

private void Button_Loaded(object sender, RoutedEventArgs e)
    {
        var button = (Button) sender;
        var command = (DelegateCommand<int?>) button.Command;
        command.RaiseCanExecuteChanged();
    }

Though something tells me it's not the best option - I will stick to this while searching for better options.

Gmoorick