WPF组合框文本搜索包含

时间:2013-03-15 08:00:19

标签: wpf search combobox

如何使用contains代替StartsWith实现我的Combobox TextSearch

<rf:ComboBox Grid.Row="1"
                         Grid.Column="5"
                         Width="200"
                         ItemsSource="{Binding Source={StaticResource AccountProvider}}"
                         DisplayMemberPath="Description"
                         SelectedValuePath="IndRekId"
                         IsEmptyItemVisible="True"
                         SelectedValue="{Binding Id, UpdateSourceTrigger=PropertyChanged}"
                         IsTextSearchEnabled="True"
                         TextSearch.TextPath="Description"
                         IsEditable="True"/>

搜索功能有效但我需要在子串上匹配

5 个答案:

答案 0 :(得分:3)

这里我在MVVM框架中有一个例子。

我的xaml文件:

<ComboBox Name="cmbContains" IsEditable="True" IsTextSearchEnabled="false" ItemsSource="{Binding pData}"  DisplayMemberPath="wTitle" Text="{Binding SearchText ,Mode=TwoWay}"  >
  <ComboBox.Triggers>
      <EventTrigger RoutedEvent="TextBoxBase.TextChanged">
          <BeginStoryboard>
              <Storyboard>
                  <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen">
                      <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
                  </BooleanAnimationUsingKeyFrames>
              </Storyboard>
          </BeginStoryboard>
      </EventTrigger>
  </ComboBox.Triggers>
</ComboBox>

我的cs文件:

//ItemsSource - pData
//There is a string attribute - wTitle included in the fooClass (DisplayMemberPath)
private ObservableCollection<fooClass> __pData;
public ObservableCollection<fooClass> pData {
    get { return __pData; }
    set { Set(() => pData, ref __pData, value);
        RaisePropertyChanged("pData");
    }
}

private string _SearchText;
public string SearchText {
    get { return this._SearchText; }
    set {
        this._SearchText = value;
        RaisePropertyChanged("SearchText");

        //Update your ItemsSource here with Linq
        pData = new ObservableCollection<fooClass>{pData.ToList().Where(.....)};
    }
}

你可以看到可编辑的comboBox绑定到字符串(SearchText) 一旦有TextChanged事件,就会显示下拉列表,并且双向绑定会更新该值。 ItemsSource在进入集合{}时在cs文件中更改;语法。

https://gist.github.com/tonywump/82e66abaf71f715c4bd45a82fce14d80

答案 1 :(得分:2)

无法用string.Contains()替换string.StartsWith()。你必须编写自定义的ComboBox。

本文可能对您有所帮助: http://www.codeproject.com/Tips/631196/ComboBox-with-Suggest-Ability-based-on-Substring-S

答案 2 :(得分:1)

此示例看起来像&#34; TextSearch&#34;

在XAML文件中,您只应向组合框添加一个属性&#34; TextContainSearch.Text&#34;:

<ComboBox ItemsSource="{Binding Model.formListIntDeviceNumbers}" SelectedItem="{Binding Path=Model.selectedDeviceNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="DeviceNumber" IsEditable="True" c:TextContainSearch.Text="DeviceNumber">

我们应该在XAML文件的标题中添加:

xmlns:c="clr-namespace:Adaptive.Controls.Extension"

* .cs文件中的C#代码:

using System;
using System.Windows;
using System.Windows.Controls;
namespace Adaptive.Controls.Extension
{
 public sealed class TextContainSearch : DependencyObject {
        public static void SetText(DependencyObject element, string text) {
            var controlSearch = element as Control;
            if (controlSearch != null)
                controlSearch.KeyUp += (sender, e) =>
                {
                    if (sender is ComboBox){
                        var control = sender as ComboBox;
                        control.IsDropDownOpen = true;
                        var oldText = control.Text;
                        foreach(var itemFromSource in control.ItemsSource){
                            if (itemFromSource != null)
                            {
                                Object simpleType = itemFromSource.GetType().GetProperty(text).GetValue(itemFromSource, null);
                                String propertOfList = simpleType as string;
                                if (!string.IsNullOrEmpty(propertOfList) && propertOfList.Contains(control.Text))
                                {
                                    control.SelectedItem = itemFromSource;
                                    control.Items.MoveCurrentTo(itemFromSource);
                                    break;
                                }
                            }
                        }
                        control.Text = oldText;
                        TextBox txt = control.Template.FindName("PART_EditableTextBox", control) as TextBox;
                        if (txt != null)
                        {
                            txt.Select(txt.Text.Length, 0);
                        }
                    }
                };
        }
    }
}

答案 3 :(得分:0)

尝试一下:

 <ComboBox Padding="3,5" MinWidth="150" SelectedItem="{Binding NewBoxRequest}"
 ItemsSource="{Binding Requests}" DisplayMemberPath="SN" IsEditable="True"
 StaysOpenOnEdit="True"
 Text="{Binding SnFilter,UpdateSourceTrigger=PropertyChanged}">
 </ComboBox>

视图模型:

    private string snFilter;

    public string SnFilter
    {
        get { return snFilter; }
        set
        {
            snFilter = value;
            RaisePropertyChanged();
            RaisePropertyChanged(nameof(Requests));
        }
    }
    private List<Request> requests;

    public List<Request> Requests
    {
        get => string.IsNullOrEmpty(SnFilter) || requests.Any(r => r.SN == SnFilter)
            ? requests
            : requests.Where(r => r.SN.Contains(SnFilter)).ToList();
        set
        {
            requests = value;
            RaisePropertyChanged();
        }
    }

答案 4 :(得分:0)

我无法在我的C#系统中使用“ Set”语法,因此这是对Wu上面答案的一个简单概括(这是在自定义控件中):

import 'core-js/stable/array/for-each';
import 'core-js/stable/array/from';
import 'core-js/stable/dom-collections';
import 'core-js/stable/object/get-own-property-symbols';

在自定义控件中:

 <ComboBox IsEditable="True" 
      IsTextSearchEnabled="false" 
      ItemsSource="{Binding pData, RelativeSource = {RelativeSource TemplatedParent}}"  
      DisplayMemberPath="description" 
      Text="{Binding SearchText , RelativeSource = {RelativeSource TemplatedParent}, Mode=TwoWay}"  >
     <ComboBox.Triggers>
                                    <EventTrigger RoutedEvent="TextBoxBase.TextChanged">
                                        <BeginStoryboard>
                                            <Storyboard>
                                                <BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen">
                                                    <DiscreteBooleanKeyFrame Value="True" KeyTime="0:0:0"/>
                                                </BooleanAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </EventTrigger>
                                </ComboBox.Triggers>
                            </ComboBox>

唯一的区别是SearchText设置器。