2013-03-15 4 views
4

가 어떻게 contains 대신WPF의 콤보 상자에서 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"/> 

StartsWith의 검색 기능을 작동를 사용하여 내 콤보 상자에서 TextSearch을 구현할 수 있습니다 포함하지만 난 문자열을 대체 할 수있는 방법이 없습니다 문자열

+0

지금까지 내가이 작업을 수행 할 수있는 유일한 방법은 콤보를 확장 컨트롤을 만들고 당신이 필요로하는 기능을 추가하는 것입니다 알고. –

답변

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 파일 : 드롭은 아래 TextChanged 이벤트를이되면

//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 문자열 (검색 텍스트) 에 구속력을 볼 수 있습니다 양방향 바인딩이 값을 업데이트합니다. 집합 {}에 들어갈 때 ItemsSource가 cs 파일에서 변경되었습니다. 통사론.

희망이 도움이됩니다. 즐겨.

나는 내 대답이 좋지 않을 수도 있다고 생각합니다. 더 많은 것을 배울 수 있도록 의견을 남겨주세요.

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

0

XAML에서

는 "TextContainSearch.Text을"콤보 상자에 하나 개의 속성을 추가해야합니다 파일 "에서 TextSearch"와 같은이 샘플보기 :

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

그리고 우리 XAML 파일의 헤더에 다음을 사용하여 추가해야합니다.

,210
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); 
         } 
        } 
       }; 
     } 
    } 
} 
관련 문제