Resolved: DelegateCommand not working

Topics: Prism v4 - Silverlight 4
Oct 31, 2011 at 11: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 ...>
            <Grid>
...
                <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?

 

 

Developer
Oct 31, 2011 at 12:49 PM

Hi,

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 
http://blogs.southworks.net/gmaliandi

Nov 1, 2011 at 4: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 4: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 9: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);
            NotifyPropertyChanged("TabClickedCommand");

Developer
Nov 1, 2011 at 1:27 PM

Hi,

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

Guido Leandro Maliandi 
http://blogs.southworks.net/gmaliandi