This is a migrated thread and some comments may be shown as answers.

mvvm canExecute InvalidateRequerySuggested

1 Answer 290 Views
Menu
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Benny
Top achievements
Rank 1
Benny asked on 30 May 2012, 10:06 AM
Hi,
I can't get the contextmenu to Reevaluate canExecute.
What is the best practise to get contextMenu to evaluate CanExecute every time it's open. (So it's enable when it's supposed to be enabled...)

MainPage.xaml
<Grid x:Name="LayoutRoot">
    <TextBlock Text="RightClick me">
    <telerik:RadContextMenu.ContextMenu>
        <telerik:RadContextMenu x:Name="ContextMenu" GotFocus="ContextMenu_GotFocus">
            <telerik:RadMenuItem Header="Remove" Name="MnuRemove" Command="{Binding Remove}" CommandParameter="{Binding guid}"  />
        </telerik:RadContextMenu>
    </telerik:RadContextMenu.ContextMenu>
    </TextBlock>
</Grid>
MainPage.xaml.cs
private void ContextMenu_GotFocus(object sender, RoutedEventArgs e)
        {
            CommandManager.InvalidateRequerySuggested();
        }
ViewModel.cs
public DelegateCommand Remove { get; private set; }
    public Viewmodel()
    {
        this.Remove = new DelegateCommand(OnRemove, CanRemove);
    }
 
    private bool CanRemove(object obj)
    {
        bool cr = ((DateTime.Now.Second % 2) == 0);
        Debug.WriteLine(cr);
        return cr;
    }
 
    private void OnRemove(object parameter)
    {
        MessageBox.Show("Remove");
    }

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivo
Telerik team
answered on 04 Jun 2012, 04:18 PM
Hello Benny,

CommandManager.InvalidateRequerySuggested() is designed to be used with RoutedCommands and not DeleagateCommands. In order to invalidate a DelegateCommand you will have to call the InvalidateCanExecute method of the concrete command.

Kind regards,
Ivo
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
Menu
Asked by
Benny
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Share this question
or