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.