Error when attached Command with a Param

Sep 20, 2012 at 10:21 PM

If I create a command RelayCommand<string>(...

Otherwise no param works fine.

I get an error.

System.InvalidCastException   at GalaSoft.MvvmLight.Command.RelayCommand`1.Execute(Object parameter)   at JulMar.Windows.Interactivity.InvokeCommand.Invoke(Object parameter)   at System.Windows.Interactivity.TriggerAction.CallInvoke(Object parameter)   at System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter)   at System.Windows.Interactivity.EventTrigger.OnEvent(Object eventArgs)   at System.Windows.Interactivity.EventTrigger.OnEventRaised(Object sender, Object e)

Coordinator
Sep 20, 2012 at 10:25 PM
Can you share the XAML binding? Specifically, what's the source of the parameter?

No specific casting is performed by the TriggerAction which means whatever the CommandParameter is will be passed directly. If you are using XML attribute syntax - CommandParameter="…", then it will always be a string..

mark

On Sep 20, 2012, at 4:22 PM, "orcities" <notifications@codeplex.com> wrote:

From: orcities

If I create a command RelayCommand<string>(...

Otherwise no param works fine.

I get an error.

System.InvalidCastException at GalaSoft.MvvmLight.Command.RelayCommand`1.Execute(Object parameter) at JulMar.Windows.Interactivity.InvokeCommand.Invoke(Object parameter) at System.Windows.Interactivity.TriggerAction.CallInvoke(Object parameter) at System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter) at System.Windows.Interactivity.EventTrigger.OnEvent(Object eventArgs) at System.Windows.Interactivity.EventTrigger.OnEventRaised(Object sender, Object e)


Sep 20, 2012 at 10:27 PM

                <ListView
                    x:Name="pendingChangesGridView"
                    AutomationProperties.AutomationId="PendingChangesGridView"
                    AutomationProperties.Name="PendingChanges"
                    TabIndex="1"
                    HorizontalAlignment="Stretch"
                    Grid.Row="1"
                    Grid.Column="0"
                        Margin="0,-45,0,0"
                            Padding="0,0,25,-40"
                    ItemTemplate="{StaticResource DashboardItemTemplate}"
                    ItemsSource="{Binding ContactList}"            
                    SelectionMode="Single"
                      ItemContainerStyle="{StaticResource ListViewItemStyle1}" 
                    IsSynchronizedWithCurrentItem="{x:Null}"
     
                    >
                    <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <Interactivity1:InvokeCommand Command="{Binding SelectItemCommand}" CommandParameter="{Binding ElementName=pendingChangesGridView,Path=SelectedItem}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
                </ListView>
SelectItemCommand = new RelayCommand<string>(s =>
                {
                    mContactListCollection.View.MoveCurrentTo(s);
                });

Sep 20, 2012 at 10:39 PM

I have found that the param being sent is a selectchangedeventargs and not the SelectedItem for the list. This seems strange because I am saying the item. Is this normal?

Coordinator
Sep 20, 2012 at 10:46 PM
That's what gets sent when the CommandParameter evaluates to null or isn't set. It passes along the specific parameter from the trigger. However, the issue you are having is the Binding itself. The attached properties don't get access to the inheritance context so you need to use a NameScopeBinding to get the proper value into your VM. It's quite easy and will fix your problem here.

See this post for details: http://julmar.com/blog/mark/?p=237

Good luck!
mark


On Sep 20, 2012, at 4:40 PM, "orcities" <notifications@codeplex.com> wrote:

From: orcities

I have found that the param being sent is a selectchangedeventargs and not the SelectedItem for the list. This seems strange because I am saying the item. Is this normal?


Sep 21, 2012 at 12:00 AM

Thank you for you input I will look into this. I have found that the ListView and GridView use the will bind with the current item both directions with the newest release. Therefore, the command behavior is not needed for my current example.

Coordinator
Sep 21, 2012 at 12:02 AM
Yes, it's only needed when item click is turned, in that case the selected item property isn't used.

Sent from my iPad

On Sep 20, 2012, at 6:00 PM, orcities <notifications@codeplex.com> wrote:

From: orcities

Thank you for you input I will look into this. I have found that the ListView and GridView use the will bind with the current item both directions with the newest release. Therefore, the command behavior is not needed for my current example.