mvvm canExecute InvalidateRequerySuggested

2 posts, 1 answers
  1. Benny
    Benny avatar
    5 posts
    Member since:
    Sep 2012

    Posted 30 May 2012 Link to this post

    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");
        }
  2. Answer
    Ivo
    Admin
    Ivo avatar
    390 posts

    Posted 04 Jun 2012 Link to this post

    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 >>

  3. DevCraft banner
Back to Top