C#: Navigate through List<Customer> with Data-bound textboxes
After trying for hours, I am highly confused by the complex data binding concepts of wpf :-/
What is the simplest approach to show the properties of my business objects (e.g. Name, Street..) in a couple of textboxes?
开发者_如何学PythonTarget is:
- User can navigate through the records (next, previous)
- Two-Way-Binding - Changes in textboxes should also change the values of the underlying propertys.
I already figured out how to bind the boxes to the properties, but how do I implement the navigation to the next/previous record?
Thanks in advance!
You'll want to look at ICollectionView methods. Here's a working example:
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>
code behind:
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();
}
精彩评论