带有 contains 的 WPF 组合框文本搜索

问题描述 投票:0回答:6

我如何使用

contains
而不是
StartsWith

来实现我的组合框文本搜索
<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"/>

搜索功能可以工作,但我需要匹配子字符串

wpf search combobox
6个回答
6
投票

这里我有一个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(.....)};
    }
}

您可以看到可编辑的组合框绑定到字符串(SearchText) 一旦发生 TextChanged 事件,就会显示下拉菜单,并且双向绑定会更新值。 当进入 set{} 时,cs 文件中的 ItemsSource 发生了变化;语法。

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


3
投票

这个示例看起来像“TextSearch”

在 XAML 文件中,您应该只向组合框“TextContainSearch.Text”添加一个属性:

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

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

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
投票

试试这个:

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

2
投票

无法用 string.Contains() 替换 string.StartsWith()。您必须编写自定义组合框。

这篇文章可能会帮助您: http://www.codeproject.com/Tips/631196/ComboBox-with-Suggest-Ability-based-on-Substring-S


1
投票

我无法让“Set”语法在我的 C# 系统中工作,所以这里是对上面 Wu 的答案的一个小起飞(这是在自定义控件中):

 <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>

在自定义控件中:

private async void _Loaded(object sender, RoutedEventArgs e)
        {
            var n = await InitializeLabTests;

            allTests = new ObservableCollection<CommonProcedure>(n);
            pData = new ObservableCollection<CommonProcedure>(n);
        }

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

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

                //Update your ItemsSource here with Linq
                pData = new ObservableCollection<CommonProcedure>
               (
                    allTests.Where(q => q.description.Contains(SearchText))
               );
            }
        }

唯一显着的区别在于 SearchText 设置器。


0
投票

ComboBox KeyUp 事件上的动态事件怎么样:

 <ComboBox DisplayMemberPath="Label" Height="37" Padding="5"                                     
           SelectedValue="{Binding Client, UpdateSourceTrigger=PropertyChanged}"
           ItemsSource="{Binding ClientList, UpdateSourceTrigger=PropertyChanged }" IsEditable="True"  TextSearch.TextPath="Label"
           KeyUp="ComboBox_KeyUp"
 />

活动:

private void ComboBox_KeyUp(object sender, KeyEventArgs e)
{
    var control = sender as ComboBox;

    if (control == null) { return; }

    if (control.Tag == null)
    {
        control.Tag = control.ItemsSource as List<ComboBoxItemViewModel>;
    }

    var filter = control.Text.ToLower();
    if (filter.Length == 0)
    {
        // If no text, show all items
        control.ItemsSource = control.Tag as List<ComboBoxItemViewModel>;
    }
    else
    {
        var dataSource = control.Tag as List<ComboBoxItemViewModel>;

        if (dataSource == null) return;

        // Filter items based on the text entered
        control.ItemsSource = dataSource.Where(item => item.Label.ToLower().Contains(filter)).ToList();
    }
}

//Just replace the List<ComboBoxItemViewModel> with your ViewModel
© www.soinside.com 2019 - 2024. All rights reserved.