2

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 Answer 1

1

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}"/> 
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry doesnt seem to solve the problem. My user control is called "ToolbarControl" (x:name) so that should be ok.
@RemotecUk I now understand your broken binding link. how to change RefreshCommand="{Binding RefreshCommand}" to RefreshCommand="{Binding ElementName="RefreshCommand" Path="Command"}" in MainWindow.xaml
Thanks you did it. Can you just explain what Path=Command does please? What is "Command" in this context?
@RemotecUk I'm glad to hear that you solved the problem and thanks to mark the answer :) Explain is simple. If you make a binding between xaml elements, the ElementName is x:Name value (you already know) and Path is one of the attribute value of the binded element. In your case, the "Command" is an attribute of the CommandBinding(x:Name="RefreshCommand") element that you want to make a binding.
@RemotecUk I think your naming rule makes you confuse. try to redefine the code CommandBinding x:Name="RefreshCommand" to CommandBinding x:Name="cmdRefresh" and RefreshCommand="{Binding ElementName=RefreshCommand, Path=Command}" to RefreshCommand="{Binding ElementName=cmdRefresh, Path=Command}". It would be clear to understand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.