DelegateCommand and Treeview in Silverlight

Dec 12, 2008 at 3:19 AM

I create DelegateCommand in my controller (MenuTemplateController)  and bound to TreeviewItem in my view (MainMenu.xaml), but It does not work.


public DelegateCommand<object> Command1 { get; private set; }

protected virtual void InitializeCommand()
          Command1 = new DelegateCommand<object>(x => OnCommand1());

 private void OnCommand1()
            var shell = container.Resolve<IShell>();
            IPresenter presenter = container.Resolve(MainMenu) as IPresenter;


 <controls:TreeViewItem Header="Employee List" cal:Commands.Command="{Binding Path=Command1}"/>

Could you give me any tip to make it work corretly! :)
Dec 12, 2008 at 12:43 PM



The Prism V2 - Drop 7 introduces a Command.Click attached property and ClickCommandBehavior that connects the Click event of a ButtonBase to an ICommand. If you need to handle the Click event of the TreeViewItem control, try modifying your XAML to include the Click attached property as follows:


<controls:TreeViewItem Header="Employee List" cal:Commands.Command.Click="{Binding Path=Command1}"/>


Please let me know if this helps.


Mariano Converti
Jan 1, 2009 at 11:50 PM
After hours (days, even) of banging my head on the keyboard, I think I can say why this doesn't work.  Also why my head hurts.  :)

If you use this with a treeview.  So the xaml looks something like:

       <controls:TreeView Grid.Row="4" x:Name="treeview" BorderThickness="0" ItemTemplate="{StaticResource accountDataTemplate}"
                       ItemsSource="{Binding NavigationItems}" cal:Command.Click="{Binding ClickCommand}"></controls:TreeView>

Then here is the problem
in the Composite.Presentation.Silverlight dll, in the Command.cs,

OnSetClickCallback is looking for a ButtonBase as shown here:

 private static void OnSetClickCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
            ButtonBase buttonBase = dependencyObject as ButtonBase;
            if (buttonBase != null)
                ClickCommandBehavior behavior = GetOrCreateBehavior(buttonBase);
                behavior.Command = e.NewValue as ICommand;

So when a treeview comes through as the dependencyObject, buttonBase will always be null.

I had actually fixed and worked with a modified command.cs of my own for all this, but ran into another issue with being able to pass the IEventAggregator along the way I needed to and am trying to work around that separate issue.  I went back to the Prism DLL's and finally just loaded them as projects and realized what was happening there.  I guess I'm a little slow to catch on.  This is my first experience with anything remotely resembling loosely coupled architecture.

Any suggestions how to get the Prism stuff to work with a treeview click?  Am I missing something here?



Jan 15, 2009 at 3:08 PM
Edited Jan 15, 2009 at 4:16 PM

Anyone have any answer to this?  The fact that this cannot work with a treeview?  I have my own workaround, but wanted to hear from anyone who knows more than I do about Prism, if this is true or not (see my last post).

Just as an additional note, I am working the same way with a data grid now, which like the treeview of course isn't a buttonbase dependency object.