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 8491223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:21:14+00:00 2026-06-10T22:21:14+00:00

I have tried two different methods of commanding in WPF from the ViewModel and

  • 0

I have tried two different methods of commanding in WPF from the ViewModel and come up short. The top method works great if I put it into the View however I was told this is bad practice. The second method I was told is the proper way to do custom commanding in MVVM however I get stuck on how to actually call/bind the command from the View.

View Model:

class MainViewModel : INotifyPropertyChanged
{

    #region INPC
    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion


     public readonly static RoutedUICommand myCommand;

    static MainViewModel()
    { 
        myCommand = new RoutedUICommand("customCommand","My Command",typeof(MainViewModel));

    }

    private void ExecutemyCommand(object sender, ExecutedRoutedEventArgs e)
    {

        MessageBox.Show("textBox1 cleared");
    }

    private void myCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }
}

On my View the code I have this which gives me an error

<Window x:Class="ConfigManager2.View.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:con="clr-namespace:ConfigManager2.Converters"
    xmlns:vm="clr-namespace:ConfigManager2.ViewModel"
    xmlns:local="clr-namespace:ConfigManager2.View"
.
.
.
<Window.CommandBindings>
    <CommandBinding
        Command="{x:Static vm:MainViewModel.myCommand}"
        CanExecute="myCommandCanExecute"
        Executed="ExecutemyCommand" />
</Window.CommandBindings>
.
.
.
 <Button Content="COMMAND ME" Height="50px"  Command="{x:Static vm:MainViewModel.myCommand}" />

The Error I am getting is ‘ConfigManager2.View.MainView’ does not contain a definition for ‘ExecutemyCommand’ and no extension method ‘ExecutemyCommand’ accepting a first argument of type ‘ConfigManager2.View.MainView’ could be found (are you missing a using directive or an assembly reference?)

I tried another method using ICommand and got stumped on how to bind this to the above button from the XAML

ViewModel:

    public ICommand ClearCommand { get; private set; }
public MainViewModel()
{
    ClearCommand= new ClearCommand(this);
}



class ClearCommand : ICommand
{
    private MainViewModel viewModel;

    public ClearCommand(MainViewModel viewModel)
    {
        this.viewModel = viewModel;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        viewModel.vmTextBox1 = String.Empty;
        MessageBox.Show("Textbox1 Cleared");
    }
}
  • 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. Editorial Team
    Editorial Team
    2026-06-10T22:21:16+00:00Added an answer on June 10, 2026 at 10:21 pm

    With the ICommand version (which I would prefer) you can bind directly to the command:

    <Button Command="{Binding ClearCommand}"/>
    

    There is no Window.CommandBinding necessary. This works, if an instance of the MainViewModel is set as the window’s or button’s DataContext.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried two different methods. The first is to specify the link in
I've tried two different methods of reusing code. I have a solution full of
I have two different-sized monitors, connected together using (I believe) TwinView. I tried System.out.println(Toolkit.getDefaultToolkit().getScreenSize());
I have a cakePHP application that is pulling data from two different databases, which
I've tried two different methods of toggling the play/pause button on my player, neither
I have tried everything, searched the net for two hours or even more and
I have tried &nbsp;&nbsp; to display two spaces in a standard output Java String.
I have two branches master and exp i tried to cherry pick some of
I have two tables: Contestant and Votes Contestant hasMany Votes I've tried doing a
Have tried to find solutions for this and can't really come up with anything.

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.