我在WPF中有一个列表框数据绑定到一个observablecollection
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding ShopList}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Grid.Row="1"
Grid.ColumnSpan="2" KeyDown="ListBox_KeyDown" KeyUp="ListBox_KeyUp"
>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17" >
<!--Replace rectangle with image-->
<Rectangle Height="50" Width="50" Stroke="Black" StrokeThickness="6" Margin="12,0,9,0"/>
<StackPanel Width="Auto">
<TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding Quantity}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
然后我有一个我想要的过滤方法叫
private void ShopItemDay_Filter(object sender, FilterEventArgs e)
{
var item = e.Item as ShopItem;
e.Accepted = item.Day == 1;
}
但我无法在列表框中找到任何属性来使用像此处所做的过滤方法http://www.galasoft.ch/mydotnet/articles/article-2007081301.aspx
答案 0 :(得分:3)
您需要在集合上创建“视图”。请参阅CollectionView.Filter的文档。该框架将为所有绑定集合创建默认视图。您可以按如下方式添加过滤器:
ICollectionView _customerView = CollectionViewSource.GetDefaultView(customers);
_customerView.Filter = CustomerFilter
private bool CustomerFilter(object item)
{
Customer customer = item as Customer;
return customer.Name.Contains( _filterString );
}
(来自此tutorial);
答案 1 :(得分:1)
正如您在链接的文章中所看到的,过滤器不是Control的属性。它是CollectionViewSource的一个属性,它是一个围绕集合的包装器。这个包装器允许排序,分组和过滤。