1

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

想法?建议?有人做过吗?

4

2 回答 2

2

我不知道@user1089031 是如何做到这一点的,但这里是任何可能感兴趣的人的工作示例(更新为@adabyron 的评论!):

视图模型.cs

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

<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控制:

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

这是因为AutoCompleteBox' 自己的SearchText属性是只读的。所以这里是这种行为的代码:

SearchTextBindBehavior.cs更新

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

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

希望有帮助。

于 2013-06-20T23:31:12.930 回答
1

这就是我最终得到的结果,对 Sevenate 的回答稍作改编,所以如果你想投票,请对他的帖子进行投票。

我使用了一个子类(AutoCompleteBox由于其他原因,我已经有了子类),它允许我创建一个包装器依赖属性来将只读SearchText(=用户通过键盘输入的内容)获取到 ViewModel - 而不是混合行为,这是一个也是完全有效的方式。

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

视图模型:

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 的子类:

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);
    }
}

xml:

<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>
于 2013-06-24T14:28:55.520 回答