我已将 WPF 弹出视图与 ListCollectionView 绑定。我有两个下一个/上一个按钮,所以当用户点击时,他可以导航到集合中。但是当用户在最后一个元素上单击下一个(或在他在第一个元素上时上一个)时,我遇到了问题。
我得到一个“System.Reflection.TargetInvocationException”。似乎 MoveCurrentToNext/Previous 尝试继续一个空项目。我已经使用断点进行了测试,并且在我的集合中有两个元素,当我在最后一个元素上时,我可以使用 MoveCurrentToNext。没有必要时是否有不使用此方法的解决方案?
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);
}
}
查看(下一个按钮的示例)
<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>
谢谢你的帮助。