WPF - 使用autocompletebox过滤DataCollection [英] WPF - Filtering a DataCollection with an autocompletebox
问题描述
我有一个视图和ViewModel工作正常。我最近添加了一个AutocompleteBox(在WPF工具包中找到),允许用户快速查找一个项目。
I have a view and ViewModel that are working perfectly. I have recently added an AutocompleteBox (found in the WPF Toolkit) which allows users to quickly look up an item.
我的看法是这样的:
- 一个包含名为People的CollectionViewSource的ItemsControl。生成完美
- 自动完成框,其中下拉列表仅显示包含用户在自动填充框中键入的值的项目。效果很好。如果我键入John,我的CollectionViewSource中名为People的人的姓名中的所有人都将出现在下拉列表中。
我的问题是:当用户从Dropdown中选择要查看的项目时,如何过滤我的ItemsControl?
My issue is: how do I filter my ItemsControl when the user selects the item he wishes to see from the Dropdown?
我的代码到目前为止在XAML中绑定数据: / p>
My code so far in XAML to bind the data:
<toolkit:AutoCompleteBox Height="25" Width="400"
Foreground="AliceBlue"
ItemsSource="{Binding People.View}"
ValueMemberPath="Name"
Text="{Binding Name}"
IsTextCompletionEnabled="True"
FilterMode="Contains"
Background="#303030">
<toolkit:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<Grid Width="360" HorizontalAlignment="Left">
<StackPanel Orientation="Vertical" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="300">
<TextBlock Text="{Binding Name}" FontWeight="SemiBold" Foreground="#25A0DA"
FontSize="14" Width="300"/>
<TextBlock Text="{Binding Status}" FontWeight="Normal" Foreground="White"
FontSize="10" Width="300"/>
</StackPanel>
</Grid>
</DataTemplate>
</toolkit:AutoCompleteBox.ItemTemplate>
</toolkit:AutoCompleteBox>
<ItemsControl x:Name="tStack" Grid.Column="0" Grid.Row="1"
ItemsSource="{Binding People.View}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
BorderThickness="0.5">
</ItemsControl>
itemsControl是样式,其中的项目的格式也是模板化/风格,但太长
The itemsControl is styled and the format of the items inside it are also templated/styled but far too long and unconstructive to post here.
推荐答案
从名称
属性设置器您的viewmodel将绑定到工具包:AutoCompleteBox.Text
,您将不得不过滤ObservableCollection,以支持您的 Collectionview
e ItemsSource
您的 ItemsControl
。
From the Name
property setter in your viewmodel to which toolkit:AutoCompleteBox.Text
is bound, you will have to filter the ObservableCollection backing your Collectionview
i,e ItemsSource
of your ItemsControl
.
如果您有您的Collectionsource与您然后您可以将过滤器应用于如下所示:
If you have your Collectionsource with you then you can have filter applied to it like below:
ICollectionView _peopleView = CollectionViewSource.GetDefaultView(peoples);
_peopleView .Filter = PeopleFilter
private bool PeopleFilter(object item)
{
People people= item as People;
return people.Name.Contains( _filterString ); //Here filter string will be your Name prperty value
}
从名称属性的Setter你可以调用 _peopleView .Refresh()
;应用过滤器
From Setter of name property you will hav to call _peopleView .Refresh()
; to apply the filter
谢谢
这篇关于WPF - 使用autocompletebox过滤DataCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!