你會想看看ICollectionView方法。 這裏有一個工作示例:
XAML:
<Window.Resources>
<x:Array x:Key="myPeoples" Type="{x:Type local:Person}">
<local:Person Name="Bob Marley" Address="123 street" />
<local:Person Name="Ted Nugent" Address="456 street" />
<local:Person Name="Ron Paul" Address="789 street" />
</x:Array>
</Window.Resources>
<DockPanel>
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal">
<Button x:Name="cmdPrevious" Click="cmdPrevious_Click">Previous</Button>
<Button x:Name="cmdNext" Click="cmdNext_Click">Next</Button>
</StackPanel>
<Grid DockPanel.Dock="Top" DataContext="{StaticResource myPeoples}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding Path=Name}" />
<TextBox Grid.Column="1" Text="{Binding Path=Address}" />
</Grid>
</DockPanel>
後面的代碼:
private void cmdPrevious_Click(object sender, RoutedEventArgs e)
{
Person[] peoples = this.FindResource("myPeoples") as Person[];
System.ComponentModel.ICollectionView collectionView = CollectionViewSource.GetDefaultView(peoples);
collectionView.MoveCurrentToPrevious();
}
private void cmdNext_Click(object sender, RoutedEventArgs e)
{
Person[] peoples = this.FindResource("myPeoples") as Person[];
System.ComponentModel.ICollectionView collectionView = CollectionViewSource.GetDefaultView(peoples);
collectionView.MoveCurrentToNext();
}