经过几个小时的努力,我对wpf的复杂数据绑定概念感到非常困惑: - /
在几个文本框中显示业务对象属性(例如Name,Street ..)的最简单方法是什么?
目标是:
我已经想出如何将这些框绑定到属性,但是如何实现导航到下一个/上一个记录?
提前致谢!
答案 0 :(得分:2)
您需要查看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();
}