How to bind button command to main window command?

Up vote 1 down vote favorite 1 share g+ share fb share tw.

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

Xaml 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 Refresh 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 doesn't 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 don't 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 doesn't 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?

C# wpf wpf-controls dependency-properties wpf-usercontrols link|improve this question edited Nov 6 '11 at 13:00H.B.43k61645 asked Nov 6 '11 at 11:03RemotecUk1,055523 68% accept rate.

Your ElementName target is wrong. Refresh fix above as below... Refresh 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.

Sorry doesn't seem to solve the problem. My user control is called "ToolbarControl" (x:name) so that should be ok. – RemotecUk Nov 6 '11 at 12:03 @RemotecUk I now understand your broken binding link.

How to change RefreshCommand="{Binding RefreshCommand}" to RefreshCommand="{Binding ElementName="RefreshCommand" Path="Command"}" in MainWindow. Xaml – Youngjae Nov 6 '11 at 12:44 Thanks you did it. Can you just explain what Path=Command does please?

What is "Command" in this context? – RemotecUk Nov 6 '11 at 14:01 @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. – Youngjae Nov 6 '11 at 15:31 @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. – Youngjae Nov 6 '11 at 15:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions