2013-03-07 10 views
6

MultiBindingText(たとえば "644 Pizza Place")として使用して "Customers"の数が多いComboBoxがあり、これは開始(CustomerNumber)から素晴らしい検索を行います。しかし、どうすれば「ピザの場所」と入力すればよいのですか?WPF ComboBox StartsWithの代わりにContainsを使用したTextSearch

<MultiBinding StringFormat="{}{0} {1}"> 
    <Binding Path="CustomerNumber" /> 
    <Binding Path="CustomerName" /> 
</MultiBinding> 

答えて

4

ComboBoxは、アイテムルックアップのためにTextSearch classを使用します。あなたは、コンボボックスにTextSearch.TextPath依存関係プロパティを設定することができます。

<ComboBox Name="cbCustomers" TextSearch.TextPath="CustomerName">...</ComboBox> 

これは、あなたが顧客名で一致させることができますが、あなたはCustomerNumberのでマッチングを失うことになります。

詳細は、次のとおりです。 入力時にComboBox.TextUpdatedメソッドが呼び出されます。このメソッドは、TextSearch.FindMatchingPrefixを呼び出して一致するアイテムを検索します。 TextSearch.FindMatchingPrefixは、string.StartsWith(..)呼び出しが使用されるメソッドです。

string.StartsWith()呼び出しやTextSearch.FindMatchingPrefixを置き換える方法はありません。したがって、string.StartsWith()をカスタムロジック(string.Containsなど)とスワップする場合は、カスタムComboBoxクラスを作成する必要があります。

0

ここでは、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(.....)}; 
    } 
} 

あなたはTextChangedイベントは、ドロップがダウンがあったら、編集可能なコンボボックスが文字列(検索テキスト) に結合されて見ることができます双方向バインディングが値を更新します。 セット{}に入る間に、ItemsSourceがcsファイルで変更されました。構文。

A gist with the code above

+0

これは優れています。私は投票していませんでした。あなたの答えはMeta http://meta.stackoverflow.com/questions/327540/was-my-edit-removing-noise-and-a-link-wrongについて話されていますので、たくさんの人が訪れるようになります。 – JRSofty

関連する問題