Linh Nguyen
Linh Nguyen

Reputation: 51

Cannot use EventTriggerBehavior with ScrollViewer's ViewChanged event

I try to use EventTriggerBehavior with ScrollViewer's ViewChanged event:

<ScrollViewer x:Name="scrollViewer">
    <i:Interaction.Behaviors>
        <core:EventTriggerBehavior EventName="ViewChanged">
             <core:InvokeCommandAction Command="{Binding AddNextCommand}"
                   CommandParameter="{Binding ElementName=scrollViewer}"/>
        </core:EventTriggerBehavior>
    </i:Interaction.Behaviors>
</ScrollViewer>

But it get an exception:

Windows.UI.Xaml.Markup.XamlParseException: 'The text associated with this error code could not be found.

Cannot add instance of type 'Microsoft.Xaml.Interactions.Core.EventTriggerBehavior' to a collection of type 'Microsoft.Xaml.Interactivity.BehaviorCollection'.

How to fix it?

I want to automate add new elements to view when ScrollViewer scrolls to bottom, are there other ways to do it? Thanks!

Upvotes: 1

Views: 475

Answers (1)

Sunteen Wu
Sunteen Wu

Reputation: 10627

Cannot add instance of type 'Microsoft.Xaml.Interactions.Core.EventTriggerBehavior' to a collection of type 'Microsoft.Xaml.Interactivity.BehaviorCollection'.

Firstly, this error indicates that the element does not have an event called ViewChanged. This is caused by you didn't allocate SourceObject for EventTriggerBehavior that the behavior is attached to the wrong element which is not scrollViewer. You should set SourceObject like follows:

<core:EventTriggerBehavior EventName="PointerPressed" SourceObject="{Binding ElementName=scrollViewer}">

But even with this, you may get another error since ViewChanged may not be supported with WindowsRuntimeMarshal.AddEventHandler. Please try to invoke the ScrollViewer.ViewChanged event directly.

I want to automate add new elements to view when ScrollViewer scrolls to bottom, are there other ways to do it?

Looks like ISupportIncrementalLoading could help. Please try it.

Upvotes: 1

Related Questions