首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF AutoCompleteBox动态排序

WPF AutoCompleteBox动态排序
EN

Stack Overflow用户
提问于 2013-03-23 12:50:08
回答 2查看 1.1K关注 0票数 1

我正在使用WPF的AutoCompleteBox,我有它的工作很好,但有一件事,我想做的是排序建议列表后,每个字母被输入到主要的TextBox。有人知道怎么做吗?我尝试在DefaultView逻辑中使用ICollectionView属性并添加SortDescriptions,但似乎没有将建议列表分阶段显示。为了确保我的集合视图排序工作正常,我将一个普通的ListBox控件和一个AutoCompleteBox控件放在同一个窗口上,并将这两个控件绑定到具有相同集合视图的相同可观察集合,普通的ListBox控件使用SortDescriptions显示正确排序的项,但AutoCompleteBox列表没有排序的项。它将它们按添加到集合中的顺序排列。

有什么想法?有什么建议吗?有人这么做过吗?

EN

回答 2

Stack Overflow用户

发布于 2013-06-21 07:31:13

我不知道@user1089031是如何做到这一点的,但这里有一个工作示例,供感兴趣的人使用(更新到@adabyron的评论!):

ViewModel.cs

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;

namespace WpfApplication12
{
    public class Item
    {
        public string Name { get; set; }

        public override string ToString()
        {
            return Name;
        }
    }

    public class ViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged = delegate {};

        private readonly ObservableCollection<Item> source;
        private readonly ICollectionView items;
        private string searchText;

        public ViewModel()
        {
            source = new ObservableCollection<Item>
                         {
                             new Item {Name = "111111111 Test abb - (1)"},
                             new Item {Name = "22222 Test - (2)"},
                             new Item {Name = "333 Test - (3)"},
                             new Item {Name = "44444 Test abc - (4)"},
                             new Item {Name = "555555 Test cde - (5)"},
                             new Item {Name = "66 Test - bbcd (6)"},
                             new Item {Name = "7 Test - cd (7)"},
                             new Item {Name = "Test - ab (8)"},
                         };

            items = new ListCollectionView(source);
        }

        public ICollectionView Items
        {
            get { return items; }
        }

        public IEnumerable<Item> ItemsSorted
        {
            get 
            {
                return string.IsNullOrEmpty(SearchText)
                        ? source
                        : (IEnumerable<Item>)source
                            .OrderBy(item => item.Name.IndexOf(SearchText,
                                StringComparison.InvariantCultureIgnoreCase));
            }
        }

        public Item Selected { get; set; }

        public string SearchText
        {
            get { return searchText; }
            set
            {
                searchText = value;
                PropertyChanged(this,
                            new PropertyChangedEventArgs("SearchText"));
                PropertyChanged(this,
                            new PropertyChangedEventArgs("ItemsSorted"));
            }
        }
    }
}

MainWindow.xaml

代码语言:javascript
复制
<Window x:Class="WpfApplication12.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"
        xmlns:wpfApplication2="clr-namespace:WpfApplication12"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        Title="MainWindow" Height="200" Width="500"
        DataContext="{DynamicResource viewModel}">

    <Window.Resources>

        <wpfApplication2:ViewModel x:Key="viewModel" />

        <DataTemplate DataType="{x:Type wpfApplication2:Item}">
            <TextBlock Text="{Binding Name}" FontFamily="Courier New" />
        </DataTemplate>

    </Window.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <controls:AutoCompleteBox
            ItemsSource="{Binding ItemsSorted}"
            FilterMode="ContainsOrdinal"
            SelectedItem="{Binding Selected, Mode=TwoWay}"
            MinimumPrefixLength="0" 
            VerticalAlignment="Top" Margin="5">

            <i:Interaction.Behaviors>
                <wpfApplication2:SearchTextBindBehavior
                            BoundSearchText="{Binding SearchText,
                                                      Mode=OneWayToSource}" />
            </i:Interaction.Behaviors>

        </controls:AutoCompleteBox>

        <ListBox Grid.Column="1"
                 ItemsSource="{Binding Items}" Margin="5" />

    </Grid>
</Window>

正如你可能注意到的,我已经向AutoCompleteBox控件添加了一个自定义行为:

代码语言:javascript
复制
<i:Interaction.Behaviors>
    <wpfApplication2:SearchTextBindBehavior
            BoundSearchText="{Binding SearchText,
                                      Mode=OneWayToSource}" />
</i:Interaction.Behaviors>

这是因为AutoCompleteBox自己的SearchText属性是只读的,所以下面是这个行为的代码:

SearchTextBindBehavior.cs (更新的)

代码语言:javascript
复制
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace WpfApplication12
{
    public class SearchTextBindBehavior : Behavior<AutoCompleteBox>
    {
        public static readonly DependencyProperty BoundSearchTextProperty =
                            DependencyProperty.Register("BoundSearchText",
                            typeof(string), typeof(SearchTextBindBehavior));

        public string BoundSearchText
        {
            get { return (string)GetValue(BoundSearchTextProperty); }
            set { SetValue(BoundSearchTextProperty, value); }
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.TextChanged += OnTextChanged;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            AssociatedObject.TextChanged -= OnTextChanged;
        }

        private void OnTextChanged(object sender, RoutedEventArgs args)
        {
            if(AssociatedObject.Text.Length == 0)
            {
                BoundSearchText = string.Empty;
                return;
            }

            if(AssociatedObject.SearchText ==
                AssociatedObject.Text.Substring(0,
                                           AssociatedObject.Text.Length - 1))
            {
                BoundSearchText = AssociatedObject.Text;
            }
        }
    }
}

注意:要使其全部正常工作,您需要从Expression Blend 4 SDK添加对System.Windows.Interactivity.dll的引用。这就是Behavior<T>和它的几个朋友居住的地方。

如果你已经安装了Expression Blend,那么你已经有了所有的SDK,不需要下载任何东西。以防万一-在我的机器上,程序集位于以下位置:

C:\Program Files\Microsoft SDKs\Expression\Blend.NETFramework\v4.0\Libraries\System.Windows.Interactivity.dll

最后,如果您有一些好的理由不添加对这个流行的官方库的引用,请随意通过普通的附加属性以“旧方式”重新实现此自定义行为。

希望这能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2013-06-24 22:28:56

这就是我最终得到的结果,对Sevenate的答案进行了轻微的修改,所以如果你想给他的帖子投赞成票,那就这么做吧。

我使用了一个子类(出于其他原因,我已经对AutoCompleteBox进行了子类化),它允许我创建一个包装器依赖属性,以获得ViewModel的只读SearchText (=用户通过键盘输入的内容),而不是混合行为,这也是一种非常有效的方式。

问题的症结在于,您应该只对SearchText而不是Text的更改应用动态排序(=如果在下拉列表中选择了某个建议,则AutoCompleteBox中显示的内容也将更改)。Sevenate引发只读ItemsSource (ItemsSorted)的PropertyChanged事件的方法是一种很好的、简洁的排序方法。

ViewModel:

代码语言:javascript
复制
public class Item
{
    public string Name { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

public class AutoCompleteBoxDynamicSortingVM : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private readonly ObservableCollection<Item> source;

    public AutoCompleteBoxDynamicSortingVM()
    {
        source = new ObservableCollection<Item>
                     {
                         new Item {Name = "111111111 Test abb - (1)"},
                         new Item {Name = "22222 Test - (2)"},
                         new Item {Name = "333 Test - (3)"},
                         new Item {Name = "44444 Test abc - (4)"},
                         new Item {Name = "555555 Test cde - (5)"},
                         new Item {Name = "66 Test - bbcd (6)"},
                         new Item {Name = "7 Test - cd (7)"},
                         new Item {Name = "Test - ab (8)"},
                     };
    }

    public IEnumerable<Item> ItemsSorted
    {
        get
        {
            return string.IsNullOrEmpty(Text) ? (IEnumerable<Item>)source :
                    source.OrderBy(item => item.Name.IndexOf(Text, StringComparison.OrdinalIgnoreCase));
        }
    }

    public Item Selected { get; set; }

    // Text that is shown in AutoCompleteBox
    private string text;
    public string Text
    {
        get { return text; }
        set { text = value; OnPropertyChanged("Text"); }
    }

    // Text that was entered by user (cannot be changed from viewmodel)
    private string searchText;
    public string SearchText
    {
        get { return searchText; }
        set
        {
            searchText = value;
            OnPropertyChanged("SearchText");
            OnPropertyChanged("ItemsSorted");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

AutoCompleteBox的子类:

代码语言:javascript
复制
public class MyAutoCompleteBox : AutoCompleteBox
{
    /// <summary>
    /// Bindable property that encapsulates the readonly property SearchText.
    /// When the viewmodel tries to set SearchText by way of EnteredText, it will fail without an exception.
    /// </summary>
    public string EnteredText
    {
        get { return (string)GetValue(EnteredTextProperty); }
        set { SetValue(EnteredTextProperty, value); } 
    }
    public static readonly DependencyProperty EnteredTextProperty = DependencyProperty.Register("EnteredText", typeof(string), typeof(MyAutoCompleteBox), new PropertyMetadata(null));


    protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
    {
        // synchronize SearchText and EnteredText (only one-way)
        if (e.Property == AutoCompleteBox.SearchTextProperty && this.EnteredText != this.SearchText)
            EnteredText = SearchText;

        base.OnPropertyChanged(e);
    }
}

Xaml:

代码语言:javascript
复制
<UserControl x:Class="WpfApplication1.Controls.AutoCompleteBoxDynamicSorting"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myctrls="clr-namespace:WpfApplication1.Controls"
        xmlns:models="clr-namespace:WpfApplication1.ViewModels"
        Height="350" Width="525"
        DataContext="{DynamicResource viewModel}">

    <UserControl.Resources>

        <models:AutoCompleteBoxDynamicSortingVM x:Key="viewModel" />

        <DataTemplate DataType="{x:Type models:Item}">
            <TextBlock Text="{Binding Path=Name}" />
        </DataTemplate>

    </UserControl.Resources>

    <Grid>
        <myctrls:MyAutoCompleteBox
            ItemsSource="{Binding ItemsSorted}"
            Text="{Binding Text, Mode=TwoWay}"
            EnteredText="{Binding SearchText, Mode=OneWayToSource}"
            FilterMode="ContainsOrdinal"
            VerticalAlignment="Top" Margin="5" />
    </Grid>
</UserControl>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15583487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档