Command binding problem - Outlook sample in SL

Topics: Prism v2 - Silverlight 2
Mar 11, 2009 at 7:24 AM
I've been banging my head against the keyboard for a while now, and I'm probably missing something really obvious...

(Should also note that I'm new to Prism, so I'm still trying to get my head around how it all hangs together)

I've putting together a test case app (just to make sure that what I want to do is all going ot work relatively easily), and I've used Erwin's Outlook sample as the basis. First thing I've done is try and port the app to Silverlight, but I can't seem to sort out the toolbar button binding (from the MainUsesCases). The buttons all load fine, but I can't seem to hook the button to the delegate ICommand - I'm using the Commands:Click.Command={Binding ...} approach, but it keeps choking on that.

The error I keep getting is "Unknown attribute Click.Command on element Button"

It's probably (hopefully) a ridiculously simple solution, and I'll look like a goose for not being able to work it out myself, but whatever.... :(

I currently have in the header

And in the toolbar content area (problem child in bold):
<Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" x:Name="ToolbarContent" >
            <ItemsControl x:Name="MainToolbar" ItemsSource="{Binding MainUseCases}"
                          HorizontalAlignment="Left" VerticalAlignment="Top" >
                        <Button Content="{Binding Path=Name}"
                                cmds:Click.Command="{Binding Path=ActivateUseCaseCommand, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
Every thing else is pretty much as is from the sample (however, it should be noted that I have ported the Outlook.Infrastructure app to VB.NET, as I'm a little more comfortable working in it)

- Matt

Mar 11, 2009 at 8:34 AM
Edited Mar 11, 2009 at 9:02 AM
Hi Matt!

One generic problem what is obvious to me is that RelativeSource property does not exists in Silverlight.

Only the following properties supported as of Silverlight 2:

On the other hand the Stock Trader Reference implementation is quite a complete sample where you can find a sample for everything what you should need for your first Prism based app (I've already experienced this :-)).

Check out WatchListView.xaml file. In that view they're using ObservableCommand as StaticResource as a workaround due to the lack of RelativeSource support in Silverlight.

Hope this helps,
Mar 11, 2009 at 5:11 PM
Hi gaffa,

You have stumbled on an interesting point, which we've also ran into when we were building Prism. "You can't do relative binding in Silverlight. "

You basically have two options to solve this 'problem'.
1. Implement the activation command on the items in the collection (in the case of the outlook style app, that would be on the IActiveAwareUseCaseControllers
2. As Attila mentiones: Place your command in a static resource. Look in the StockTrader RI in the PositionSummaryView.xaml (the BuyCommand and SellCommand use this trick)

HOpe this helps.
Mar 11, 2009 at 6:52 PM
I have run into the same binding issue.  I looked at the PositionSummary and have implemented the ObservableCommand class in my Infrastructure module.

namespace FuzzyConfig.Infrastructure


    public class ObservableCommand : ObservableObject<ICommand>




I added the Resources to  my user control


        <infrastructure:ObservableCommand x:Key="RemoveSetCommand" />

        <infrastructure:ObservableCommand x:Key="EditSetCommand" />


Below is the mark up for my ListBox

            <ListBox x:Name="InputSetList" MinHeight="150" 

                     ItemsSource="{Binding FuzzyBlock.InputSets}"

                     SelectedItem="{Binding SelectedFuzzySet, Mode=TwoWay}">



                        <Border BorderThickness="1" BorderBrush="BurlyWood">

                            <StackPanel x:Name="InputSetPanel" Orientation="Horizontal" Margin="2 2">

                                <TextBlock x:Name="Name" Text="{Binding SetName }" Width="240"/>

                                <Button x:Name="RemoveSet" Content="-X-" Click="RemoveSet_Click" Tag="{Binding Current}" />

                                <Button x:Name="EditSet" Content="-E-" 

                                        commands:Click.Command="{Binding Path=Value, Source={StaticResource EditSetCommand}}"

                                        commands:Click.CommandParameter="{Binding Current}"







The EditSet command button displays for each item in the list.  But when I click the button, the EventHandler attached to the ViewModel code is not being executed.  My previous workaround was to use the code behind as shown in the RemoveSet Command.

To complete the remaining of my code, in the ViewModel I have declared the commands as shown below.

        public ICommand RemoveSetCommand { get; private set; }

        public ICommand EditSetCommand { get; private set; }

         this.RemoveSetCommand = new DelegateCommand<IFuzzySet>(OnRemoveSet);

         this.EditSetCommand = new DelegateCommand<IFuzzySet>(OnEditSet);

        public void OnRemoveSet(IFuzzySet fuzzySet)




        public void OnEditSet(IFuzzySet fuzzySet)




 What am I missing?

Thanks in advance

Mar 11, 2009 at 7:11 PM
Edited Mar 11, 2009 at 7:41 PM
Check out PositionSummaryView.xaml.cs for the Set accessor for the Model property.

That's where the bound member Value get it's real value from the PositionSummaryPresentationModel's constructor:

View.Model = this;

hope this helps,
Mar 11, 2009 at 7:57 PM
Thanks attiah,

Feeling very foolish for not thinking to look at the code behind, but it makes perfect sense and it's working.

But this makes a more interesting point.  Since the solution of this involved adding code to the code behind to use the binding syntax, and my working example was to simply add the click event handlers to the code behind that called the event handler on the ViewModel it appears to be a toss up as to which work around is better.  Certainly the Button wire up is simpler from an implementation standpoint, considering declaring the static resources for the user control and having to add the setter logic to the code behind.

Mar 11, 2009 at 10:49 PM
Thanks to everyone who took the time to reply... looking a bit closer, I must have been tired, as I knew that the RelativeSource wasn't in Silverlight.... that wouldn't have solved my problem, but I probably wouldn't have felt so stupid either.

I'll have a play around with the suggestions above and see what I can sort out. I'm really trying to keep my View code behind empty to stop business logic leaking into it...

- Matt