Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 155671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:11:48+00:00 2026-05-11T10:11:48+00:00

I have a ViewModel class which i want to respond to the built in

  • 0

I have a ViewModel class which i want to respond to the built in Refresh command whic is fired from a button but i’m not sure how to declare the CommandTarget.

Briefly, my code is as below

The ViewModel constructor and CanExecute and Executed event handlers –

    public ViewModel()     {         CommandBinding binding = new CommandBinding(NavigationCommands.Refresh, CommandHandler);         binding.CanExecute += new CanExecuteRoutedEventHandler(binding_CanExecute);         binding.Executed += new ExecutedRoutedEventHandler(binding_Executed);         CommandManager.RegisterClassCommandBinding(typeof(ViewModel), binding);     }     void binding_Executed(object sender, ExecutedRoutedEventArgs e)     {         Debug.Print('Refreshing...');     }      void binding_CanExecute(object sender, CanExecuteRoutedEventArgs e)     {         e.CanExecute = true;     } 

The markup is –

<Button Command='Refresh'>refresh</Button> 

Now, I’ve tried setting the CommandTarget on this button to {Binding Source={StaticResource ViewModel}} but i get a runtime saying Cannot convert the value in attribute 'CommandTarget' to object of type 'System.Windows.IInputElement'.

I’m new to commands so it’s entirely possible I’m all kinds of wrong here. Anyhelp would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:11:49+00:00Added an answer on May 11, 2026 at 10:11 am

    RoutedCommands and MVVM do not mix. RoutedCommands are tied to the visual tree and to rely on WPF’s CommandBindings collection. You should implement your own ICommand classes that work with the MVVM pattern. Take a look at Prism’s implementations for starters.

    In my own MVVM projects, I have a couple of command implementations:

    • DelegateCommand. Calls provided delegates to determine whether the command can execute, and to execute the command.
    • ActiveAwareCommand. Works in conjunction with an interface (IActiveAware) and sends command executions to the currently active item. Multiple active aware implementations register themselves with the command, and the command automatically routes CanExecute / Execute calls to the currently active item.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ViewModel class which I want to resolve via the unity ServiceLocator,
I have the following ViewModel, which is the base class of other ViewModels that
I have two classes (MVC view model) which inherits from one abstract base class.
I have a class derived from ItemsControl in which I implement my own selection-algorithm
I have viewmodel which i am contructing in a Create get action, public class
I have a ViewModel class that contains a list of points, and I am
I have this ViewModel public class CustomerSuscribeViewModel : IValidatableObject { [DataMember(IsRequired = true)] [DataType(DataType.Text)]
I have the following ViewModel public class RecommendationModel { public List<CheckBoxItem> CheckBoxList { get;
I have a class that roughly looks like this: public class ViewModel { public
I have a ViewModel defined like this: public class LocationTreeViewModel<TTree> : ObservableCollection<TTree>, INotifyPropertyChanged TTree

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.