Resolved: DelegateCommand not working

Topics: Prism v4 - Silverlight 4
Oct 31, 2011 at 10:01 AM

I dunno why my DelegateCommand not working.

  • I adopt viewModel-1st design.
  • View is being registerType into container at module loading

my ViewModel:

public abstract class ViewModelBase<TIView ....
        public ViewModelBase(... TIView view)
            view.DataContext = this;
            View = view;
public class MyViewModel : ViewModelBase<IMyView ...
        public DelegateCommand<object> TabClickedCommand { get; set; }

        public MyViewModel(... Views.IMyView view)
            : base(... view)
            view.ViewModelBase = this;

            this.TabClickedCommand = new DelegateCommand<object>(this.TabClicked, this.CanTabClick);

        public bool CanTabClick(object data) ...
        public void TabClicked(object data) ...
This is my View:
    <ctk:DockPanel ...>
        <Border ...>
                <Button ... prism:Click.Command="{Binding TabClickedCommand}" />

I have debug into the view, it's DataContext is referencing the correct and expected VM instance, but the DelegateWork is never worked.
What am I missing?



Oct 31, 2011 at 11:49 AM


In case the command binding is placed inside a data template, you might find this thread useful, where the problem is discussed, along with possible solutions to it.

Otherwise, it would be helpful if you could provide us with a repro sample application that portrays this problem.

I hope you find this helpful.

Guido Leandro Maliandi

Nov 1, 2011 at 3:29 AM

I did further testing, I can get it work, if I explicitly set the binding on the ui control at the view loaded event:

        void MyView_Loaded(object sender, RoutedEventArgs e)
            System.Windows.Data.Binding z = new System.Windows.Data.Binding();
            z.Source = this.DataContext;
            z.Path = new PropertyPath("TabClickedCommand");

            this.btn1.SetBinding(Microsoft.Practices.Prism.Commands.Click.CommandProperty, z);

So, code behind worked, but xaml not. I guess its somekind of timing/sync design issue.

How can I fixed this without the need to explicitly bind at code behind?

Nov 1, 2011 at 3:33 AM

i think its properly about the Source.

Which I explicit set in code behind, but not in XAML. But I dunno the proper syntax doing it in XAML.

I tried prism:Click.Command="{Binding TabClickedCommand, Source=DataContext}" won't work either. properly need to lookup resource something like that.

Nov 1, 2011 at 8:35 AM

ok, i figured this out. I didn't raise property changed event.

fixed by:

            this.TabClickedCommand = new DelegateCommand<object>(this.TabClicked, this.CanTabClick);

Nov 1, 2011 at 12:27 PM


Thank you for sharing your findings with the rest of the community.

Guido Leandro Maliandi