0

I have binded a WPF popup view with a ListCollectionView. I have two button next/previous on it, so when a user click, he could navigate into the collection. But I have problem when the user click on next when he's on the last element (or previous when he's on the first element).

I get a 'System.Reflection.TargetInvocationException'. It's seems that MoveCurrentToNext/Previous try to go on an empty item. I've made a test with breakpoint, and with two elements in my collection, I could use MoveCurrentToNext, when I'm on the last element. Is there a solution to not use this method when it's not necessary ?

public class PopupViewModel : Screen
{

    private readonly PopupManager manager = new PopupManager();
    public ListCollectionView AlertCollectionView { get; set; }

    public void PreviousRecordExecute()
    {
       this.AlertCollectionView.MoveCurrentToPrevious();    
    }

    public void NextRecordExecute()
    {
        this.AlertCollectionView.MoveCurrentToNext();
    }

    private FeedItem CurrentAlert
    {
        get 
        {
            return AlertCollectionView.CurrentItem as FeedItem; 
        }
        set
        {
            AlertCollectionView.MoveCurrentTo(value);
            NotifyOfPropertyChange();
        }
    }

    [ImportingConstructor]
    public PopupViewModel()
    {
        manager.GetPopData().CollectionChanged += (s, e) => AlertCollectionView.Refresh();
        AlertCollectionView = new ListCollectionView(manager.GetPopData());
        AlertCollectionView.MoveCurrentToPosition(0);
    }
}

View (example for Next Button)

<ItemsControl Margin="369,87,10,304">
    <Button Height="19" Width="19" BorderThickness="0" Margin="1,0" BorderBrush="{x:Null}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <cal:ActionMessage MethodName="NextRecordExecute"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        <Button.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFFFC500"/>
                <GradientStop Color="#FFF1E29E" Offset="1"/>
            </LinearGradientBrush>
        </Button.Background>
    </Button>
</ItemsControl>

Thanks for any help.

Konamiman
  • 49,681
  • 17
  • 108
  • 138
mrplume
  • 183
  • 1
  • 3
  • 18
  • Please provide [a good, _minimal_, _complete_ code example](https://stackoverflow.com/help/mcve) that reliably reproduces the problem. Barring that, debug it yourself by looking at the `InnerException` property value of the `TargetInvocationException` that is being thrown. – Peter Duniho Jul 01 '15 at 17:59
  • I asked this question because I see in console that when I click on the next button, I go to next element even if it's the last element. So, I'm asking if I must create a function to don't execute MoveCurrentTo... if there is no element left, or if ListCollectionView have method to do that. – mrplume Jul 02 '15 at 07:27

0 Answers0