WPF学习笔记11. Binding (4)
2010-10-11 16:33:44 来源:本站整理(3) 过滤
利用 CollectionViewSource.Filter 委托属性,我们可以对数据源做出过滤处理。比如过滤掉全部女性。
Window1.xaml.cs
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
var personals = this.FindResource("personals");
var view = CollectionViewSource.GetDefaultView(personals);
view.Filter = o =>
{
return (o as Personal).Sex != Sex.Female;
};
}
}
(MSDN 文档好像对不上)
(4) 导航
这个功能很常用,尤其是在数据库系统开发中。不过需要注意的是我们必须确保 IsSynchronizedWithCurrentItem = true。
Window1.xaml
<Window x:Class="Learn.WPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Learn.WPF"
Title="Window1">
<Window.Resources>
<my:PersonalList x:Key="personals" >
<my:Personal Name="Tom" Age="15" Sex="Male" />
<my:Personal Name="Mary" Age="11" Sex="Female" />
<my:Personal Name="Jack" Age="12" Sex="Male" />
<my:Personal Name="Smith" Age="10" Sex="Male" />
<my:Personal Name="Li." Age="8" Sex="Female" />
</my:PersonalList>
</Window.Resources>
<Grid>
<StackPanel DataContext="{StaticResource personals}">
<ListBox x:Name="listbox1" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock>,</TextBlock>
<TextBlock Text="{Binding Path=Age}" />
<TextBlock>,</TextBlock>
<TextBlock Text="{Binding Path=Sex}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Label Content="{Binding /Name}" />
<Label Content="{Binding /Age}"/>
<Button x:Name="btnFirst" Click="ButtonClick" Content="First" />
<Button x:Name="btnPrev" Click="ButtonClick" Content="Prev" />
<Button x:Name="btnNext" Click="ButtonClick" Content="Next" />
<Button x:Name="btnLast" Click="ButtonClick" Content="Last" />
<Button x:Name="btnPostion" Click="ButtonClick" Content="Position: 2" Tag="2" />
</StackPanel>
</Grid>
</Window>
更多精彩
赞助商链接