2013-03-15 29 views
4

如何使用contains代替StartsWith与WPF组合框的文本搜索包含

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

搜索功能的作品我实现了我的组合框文本搜索,但我需要匹配子

+0

据我知道做到这一点的唯一方法是创建一个扩展组合框控件,并添加您需要的功能。 – 2013-03-15 08:08:48

回答

2

在这里,我在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

0

此示例看起来像 “文本搜索”

在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文件的标题中使用:

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