【问题标题】:WPF - Filtering a DataCollection with an autocompleteboxWPF - 使用自动完成框过滤 DataCollection
【发布时间】:2013-09-06 09:45:04
【问题描述】:

我有一个运行良好的视图和 ViewModel。我最近添加了一个 AutocompleteBox(可在 WPF 工具包中找到),它允许用户快速查找项目。

我的看法是这样的:

  • 一个 ItemsControl 包含我名为 People 的 CollectionViewSource。完美生成
  • 一个自动完成框,其中的下拉菜单仅显示包含用户在自动完成框中键入的值的项目。效果很好。如果我键入 John,我的 CollectionViewSource 中名为 People 且名称中包含 John 一词的所有人员都会出现在下拉列表中。

我的问题是:当用户从下拉列表中选择他希望看到的项目时,我如何过滤我的 ItemsControl?

到目前为止,我在 XAML 中绑定数据的代码:

<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 是样式化的,其中项目的格式也是模板化/样式化的,但是太长且不具有建设性,无法在此处发布。

【问题讨论】:

    标签: wpf xaml data-binding filter autocompletebox


    【解决方案1】:

    toolkit:AutoCompleteBox.Text 绑定到的视图模型中的Name 属性设置器,您必须过滤支持您的Collectionview 的ObservableCollection,即ItemsSourceItemsSource

    如果你有你的 Collectionsource ,那么你可以像下面这样对其应用过滤器:

    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
    }
    

    从名称属性的设置器中,您将不得不调用_peopleView .Refresh();应用过滤器

    谢谢

    【讨论】:

    • 我想我理解你的代码的概念,但我到底用什么来代替 _filterString ?我是否必须使用 get 创建一个公共字符串?放;属性?
    • 您将输入用户选择的“名称”的值
    猜你喜欢
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多