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

  • Home
  • SEARCH
  • 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 6773835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:42:55+00:00 2026-05-26T15:42:55+00:00

Im attempting to bind a button in a user control to a command defined

  • 0

Im attempting to bind a button in a user control to a command defined in my applications main window. Just can’t seem to get it to work. The CanExecute method is never called and neither is the code when the button is clicked..

MainWindow.xaml

<Window.CommandBindings>
    <CommandBinding x:Name="RefreshCommand" 
                    Command="AppCommands:DataCommands.Refresh"
                    Executed="Refresh_Executed"
                    CanExecute="Refresh_CanExecute" />
</Window.CommandBindings>

<uc:Toolbar x:Name="MainToolbar" Grid.Row="0" RefreshCommand="{Binding RefreshCommand}"/>

MainWindow.xaml.cs

private void Refresh_Executed(object sender, ExecutedRoutedEventArgs e)
{

}

private void Refresh_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = false;
}

Additionally this is done in MainWindow’s constructor…

MainToolbar.DataContext = this;

Toolbar.xaml

<Button x:Name="btnRefresh" Command="{Binding RefreshCommand, ElementName=ToolbarControl}">Refresh</Button>

Toolbar.xaml.cs

#region Command Bindings


public static readonly DependencyProperty RefreshCommandProperty =
            DependencyProperty.Register("RefreshCommand", typeof(ICommand), typeof(Toolbar), new UIPropertyMetadata(null));

public ICommand RefreshCommand
{
    get { return (ICommand)GetValue(RefreshCommandProperty); }
    set { SetValue(RefreshCommandProperty, value); }
}

#endregion

If anyone can see why this doesnt work that would be appreciated. I think Ive got everything hooked up ok. However, I have put breakpoints on my event handlers for the button command in the main window and they just dont get called. The only real area of confusion is in my MainWindow.xaml, am I using the correct binding expression to bind the user controls property to my actual command?

Note: At present the CanExecute is set to false as I want to initially disable the button (but this also doesnt work).

Update:
This is clearly the source of the problem…

System.Windows.Data Error: 40 : BindingExpression path error: 'RefreshCommand' property not found on 'object' ''MainWindow' (Name='')'. BindingExpression:Path=RefreshCommand; DataItem='MainWindow' (Name=''); target element is 'Toolbar' (Name='MainToolbar'); target property is 'RefreshCommand' (type 'ICommand')

… but how to fix it?

  • 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-26T15:42:56+00:00Added an answer on May 26, 2026 at 3:42 pm

    Your ElementName target is wrong.

    <Button x:Name="btnRefresh" Command="{Binding RefreshCommand, ElementName=ToolbarControl}">Refresh</Button> 
    

    fix above as below…

    <Button x:Name="btnRefresh" Command="{Binding RefreshCommand, ElementName=MainToolbar}">Refresh</Button> 
    

    The ElementName has to be a value of x:Name or Name.

    — EDIT (Above codes are wrongly pointed) —

    As you know, in Element-to-Element interaction in Xaml, ElementName has to be defined.

    The RefreshCommand of MainToolbar in MainWindow.xaml has to be binded to x:Name in CommandBinding.
    In other words, you did not specify the element name, so the binding target is wrongly pointed.

    Try the code below.

    <uc:Toolbar x:Name="MainToolbar" Grid.Row="0" RefreshCommand="{Binding ElementName=RefreshCommand, Path=Command}"/> 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to bind a ListView control to a DataTable , but the
I am attempting to grep out bind for a specific user from an LDAP
I am attempting to bind a WPF textbox's Maxlength property to a known constant
I'm attempting to bind a DependancyProperty in one of my usercontrols to the Width
<RichTextBox x:Name=OrigText Margin=0,0,8,0 d:LayoutOverrides=Width/> <Button x:Name=OrigFileBrowse Command={Binding BrowseCommand} CommandParameter={Binding ElementName=OrigText, Path=Document} HorizontalAlignment=Center Margin=0,0,8,2.442 Width=75
This one seems painfully obvious to me, but for some reason I can't get
Im attempting to bind to the output of a method. Now I've seen examples
Without attempting to bind it
I'm attempting to bind a Dictionary to a ListView who's item template constist of
I'm attempting to bind a RadPanelBar to a flat data source in Silverlight. All

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.