简体   繁体   中英

CommandParameter always passes EventArgs with ElementName binding, no actual binded value

So, with the Behavior SDK I want to bind a Pivot event to my viewmodel. The binding looks like this:

<Pivot Grid.Row="1" x:Name="pvtMain"
       ItemsSource="{Binding Items}"
       HeaderTemplate="{StaticResource PivotHeaderTemplate}">
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="PivotItemLoading">
            <Core:InvokeCommandAction Command="{Binding LoadAdditionalData}"/>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>

</Pivot>

The action that is performed in the Viewmodel looks like this:

private void _CommandLoadAdditionalData(object parameter) {
    var test = (parameter as PivotItemEventArgs);      
}

The problem is as follows: I'm getting the error: type or namespace name 'PivotItemEventArgs' could not be found (are you missing a using directive or an assembly reference?) .

But when I run the project everything works just fine. When dig a little deeper, PivotItemEventArgs does reside in the Windows.UI.Xaml.Controls, but it won't be found in the Shared project.

I'm guessing this happens because a pivot item is no Windows 8 control.

Now I just want the SelectedItem to be passed, instead of EventArgs. I changed my CommandParameter to the following:

<Core:InvokeCommandAction Command="{Binding LoadAdditionalData}"
                                      CommandParameter="{Binding ElementName=pvtMain, Path=SelectedItem}" />

Still, the value is still PivotItemEventArgs. Am I missing something?

So after some struggling, I've stumbled upon the 'solution'.

I bound the command on the PivotItemLoading event. Somehow, at the first load event trigger, the parameter is always of type PivotItemEventArgs .

However, after the second loading, the correct object (the SelectedItem ) is being passed along!

I don't know the exact reason, but it's good to know this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM