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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:59:26+00:00 2026-05-31T20:59:26+00:00

I am try to Binding a ICommand property on a Button Command from MainWindow.

  • 0

I am try to Binding a ICommand property on a Button Command from MainWindow. But it`s not working.
Here is sample code which I try for this.

C# Code :

public partial class MainWindow : Window
{
    private ICommand _StartButtonCommand;

    public ICommand StartButtonCommand
    {
        get{ return this._StartButtonCommand;}
        set 
        {
            if (this._StartButtonCommand!=value)
            {
                this._StartButtonCommand = value;
            }
        }
    }
     public MainWindow()
     {
        InitializeComponent();
        this.StartButtonCommand = new ReplayCommand(new Action<object>(startButtonCommandEx));
     }
     private void startButtonCommandEx(object obj)
     {
         MessageBox.Show("Done");
     }

    protected class ReplayCommand : ICommand
    {
        private Action<object> _action;

        public ReplayCommand(Action<object> action)
        {
            this._action = action;
        }
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            try
            {
                if (parameter!=null)
                {
                    this._action(parameter);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "AutoShutdown", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
    }

XAML :

 <Button x:Name="buttonStrat" Content="Start" HorizontalAlignment="Left" Width="84.274" Height="39.246" Command="{Binding StartButtonCommand, ElementName=window}"/>

Actually I want to access UI Elements Like DataGridView SelectedItem Property or any other UI property using ICommand thats why i am writing ICommand in MainWindow Class.
I don
t know this is right way or wrong way. I just try it and i am not success. If it is wrong way please advice me what is right way and how to do it.

Thank`s for advice.

  • 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-05-31T20:59:27+00:00Added an answer on May 31, 2026 at 8:59 pm

    First, you didn’t set the DataContext on the button:

    buttonStrat.DataContext = this;
    

    And in the xaml use this:

    Command="{Binding StartButtonCommand}"
    

    Also to make your code shorter you could change your property to this:

    public ICommand StartButtonCommand
    {
        get { return new ReplayCommand(new Action<object>(startButtonCommandEx)); }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to use binding with an attached property. But can't get it working.
The WPF CommandManager allows you to do the following (pseudo-ish-code): <Button Name=SomeButton Command={Binding Path=ViewModelCommand}/>
Try to see which cast is faster (not necessary better): new c++ case or
I am trying to retrieve Data Binding information from a ComboBox. I try this
I try to bind XAML ListBoxItem using Code but doesn't seem work In my
I try to do binding from DataTemplate to myGridViewColumn. I want to display custom
i am trying to binding a very simple property to a TextBlock, but I
The checkbox data-binding value is not changing when i try to trigger the click
Try the following code public enum Color { Blue=1, Red=2, Green=3 } public List<Color>
I'm using the ICommand interface to perform binding on a couple of buttons in

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.