得到当前项
1 void birthdayButton_Click(object sender, RoutedEventArgs e) { 2 // Get the current person out of the collection view 3 People people = (People)this.FindResource("Family"); 4 ICollectionView view = 5 CollectionViewSource.GetDefaultView(people); 6 Person person = (Person)view.CurrentItem; 7 ++person.Age; 8 MessageBox.Show(...); 9 }Navigating between items 改变当前项
//得到数据的视图 ICollectionView GetFamilyView( ) { People people = (People)this.FindResource("Family"); return CollectionViewSource.GetDefaultView(people); }
1 //上一条记录 2 void backButton_Click(object sender, RoutedEventArgs e) { 3 ICollectionView view = GetFamilyView(); 4 view.MoveCurrentToPrevious(); 5 if( view.IsCurrentBeforeFirst ) { 6 view.MoveCurrentToFirst(); 7 } 8 }
//下一条记录 void forwardButton_Click(object sender, RoutedEventArgs e) { ICollectionView view = GetFamilyView(); view.MoveCurrentToNext(); if( view.IsCurrentAfterLast ) { view.MoveCurrentToLast(); } }List Data Targets
<ListBox x:Name="lb" ItemsSource="{Binding}" DisplayMemberPath="Name" SelectedValuePath="Age" IsSynchronizedWithCurrentItem="True"/>ListBox的ItemsSource属性是一个没有路径的Binding ,相当于“绑定到整个当前对象”,即从父控件继承上下文数据 DataContent 将ListBox类的IsSynchronizedWithCurrentItem属性设置为True ,这样当列表框的选定项发生变化时,它会更新视图中的当前项(反之亦然)
<ComboBox ItemsSource="{StaticResource NamedAgeLookup}" DisplayMemberPath="NameForAge" SelectedValuePath="AgeId" SelectedValue="{Binding Path=Age}" />
注意:绑定的“Age"属性在当前数据源中是没有的,他会找上级数据源,当上级数据源的当前项变更时,ConboBox会显示 上级的当前项Age值=AgeId值对应的项。
标签:绑定,people,List,People,Binding,ICollectionView,数据源,Data,view From: https://www.cnblogs.com/friend/p/17043143.html