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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:24:50+00:00 2026-06-12T09:24:50+00:00

I am having a problem with xaml … a button I have created is

  • 0

I am having a problem with xaml … a button I have created is not enable. here is the xaml part:

<Button Margin="0,2,2,2" Width="70" Content="Line" 
        Command="{x:Static local:DrawingCanvas.DrawShape}"
        CommandTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
            AncestorType={x:Type Window}}, Path=DrawingTarget}"
        CommandParameter="Line">           
</Button>

Before Constructor it goes:

    public static RoutedCommand DrawShape = new RoutedCommand();

in ctor I have:

this.CommandBindings.Add(new CommandBinding(DrawingCanvas.DrawShape, DrawShape_Executed, DrawShapeCanExecute));

Then I have:

private void DrawShapeCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;  **//Isn't this enough to make it enable?**
    en.Handled = true;

}

private void DrawShape_Executed(object sender, ExecutedRoutedEventArgs e)
{
    switch (e.Parameter.ToString())
    {
        case "Line":
            //some code here (incomplete yet)
            break;
    }

When I remove the first line (Command="{x:Static ...}") in the block it gets enable again!

  • 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-12T09:24:50+00:00Added an answer on June 12, 2026 at 9:24 am

    Be sure the CanExecute property of that command is returning true. If it returns false, it automatically disables the control that utilizes that command.

    Can execute should return a bool, I’m a little surprised that doesn’t give a compile error. Anyway try to change it to this.

    private bool DrawShapeCanExecute(object sender, CanExecuteRoutedEventArgs e) 
    {
        return true; 
    }
    

    EDIT:

    Ok since you just revealed all you want is a simple button that executes a command here’s a very simple implementation copied from one of my recent projects. First define this class somewhere.

    public class GenericCommand : ICommand
    {
        public event EventHandler CanExecuteChanged { add{} remove{} } 
    
        public Predicate<object> CanExecuteFunc{ get; set; }
    
        public Action<object> ExecuteFunc{ get; set; }
    
        public bool CanExecute(object parameter)
        {
            return CanExecuteFunc(parameter);
        }
    
        public void Execute(object parameter)
        {
            ExecuteFunc(parameter);
        }
    }
    

    Next define a command in your view model and define both the properties I created in the generic command (it’s just the basic stuff that comes along with implementing the ICommand interface).

     public GenericCommand MyCommand { get; set; }
    
     MyCommand = new GenericCommand();
     MyCommand.CanExecuteFunc = obj => true;
     MyCommand.ExecuteFunc = obj => MyMethod;
    
     private void MyMethod(object parameter)
     {
          //define your command here
     }
    

    Then just wire up the button to your command.

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

    If this is all too much for you (MVVM does require a little extra initial setup). You can always just do this…

    <Button Click="MyMethod"/>
    

    private void MyMethod(object sender, RoutedEventArgs e)
    {
        //define your method
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
I'm having a problem with the default Button in the xaml code below: <Window
I am having a kinda strange problem. Here is the situation. I have a
Im having problem in my UIscrollView ,this is what I have done: Whenever a
I'm having problem with datagrid view. I have attached an image with the code
hello I am having problem related to https:// . I have used FB.getLoginStatus(function(response) function
I am having major problem in Data Binding. I have a stackpanel with an
I'm having a strange problem with a colour animation. I have a list of
I'm having a problem where I have a DataGridColumnHeader that is receiving text with
i am having a problem with the IDataErrorInfo, here is a sample public partial

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.