2009-08-16 11 views
1

私はWPF辞書アプリを開発しています。 XAMLコードにリストボックスとテキストボックスがあり、テキストボックスのユーザー入力に基づいてデータセットをフィルタ処理する必要があり、リストボックスには関連する行のみを表示する必要があります。 たとえば、ユーザーがテキストボックスの 't'を入力すると、リストボックスにはテレビのような単語しか表示されません。 問題は、フォームが読み込まれるときにリストボックスが50語で埋められますが、テキストボックス、リストボックスが空白になります。 データセット自体をフィルタリングしようとしましたが、いつもエラーが表示されてしまいましたか?どうやってやるの?wpfでデータセットをフィルタリングする

私のコードはここに

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Shapes; 
using System.Data; 
using System.Data.OleDb; 
using System.ComponentModel; 

namespace DictionaryM 
{ 
    /// <summary> 
    /// Interaction logic for Dictionary.xaml 
    /// </summary> 
    public partial class Dictionary : Window 
    { 
     public ICollectionView view; 

     public Dictionary() 
     { 
      InitializeComponent(); 

      BindData(); 

      } 
     public void BindData() 

     { 


      OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\CellBiology.mdb;Persist Security Info=True"); 
      con.Open(); 
      string sql = "Select Word from Dictionary"; 
      OleDbCommand cmd = new OleDbCommand(sql, con); 
      OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      try 
      { 


       da.Fill(ds, "Word"); 
       listBox1.DataContext = ds; 



      } 
      catch (Exception ex) 
      { 

       label1.Content = ex.Message; 

      } 


     private void textBox1_TextChanged(object sender, TextChangedEventArgs e) 
     { 

       // i dont know what will i code here? 



     } 


    } 

thanks everyone who response this question 

答えて

0
private void textBox1_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    listBox1.Items.Filter = 
      (value) => 
      { 
       DataRow row = value as DataRow; 
       if (row != null) 
       { 
        if (row["Word"].ToLower().StartsWith(textBox1.Text.ToLower()) 
         return true; 
       } 
       return false; 
      }; 
} 

(実際に私は多分それはDataRowView、ないのDataRowだ、項目の実際の型についてはよく分からない...)

+0

ご返信ありがとうございます –

0
listBox1.Items.CanFilter 
です

は、ItemsがDataSetの場合はfalseを返し、動作しません。

関連する問題