2012-04-29 22 views
0

私はカスタムのユーザーコントロールを開発しようとしています。私のユーザーコントロールでは、リストボックスとテキストボックスの2つのコントロールを使用します。テキストボックスは、リストボックス内の項目をフィルタリングするために使用されます。これを行うには、私のフィルタメソッドの問題に直面しています。私のフィルタメソッドでは、オブジェクトをItemSource型にキャストする必要があります。しかし、私はそれをキャストする方法を理解していません。C#:オブジェクトをItemSourceデータ型にキャストする方法

public static readonly DependencyProperty ItemSourceProperty = DependencyProperty.Register("ItemSourrce", typeof(IEnumerable), typeof(SSSearchListBox), new PropertyMetadata(new PropertyChangedCallback(OnItemsSourcePropertyChanged))); 

    private static void OnItemsSourcePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
    { 
     var control = sender as SSSearchListBox; 
     if (control != null) 
      control.OnItemsSourceChanged((IEnumerable)e.OldValue, (IEnumerable)e.NewValue); 

    } 

    private void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue) 
    { 
     // Remove handler for oldValue.CollectionChanged 
     var oldValueINotifyCollectionChanged = newValue as INotifyCollectionChanged; 

     if (null != oldValueINotifyCollectionChanged) 
     { 
      oldValueINotifyCollectionChanged.CollectionChanged -= new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged); 
     } 
     // Add handler for newValue.CollectionChanged (if possible) 
     var newValueINotifyCollectionChanged = newValue as INotifyCollectionChanged; 
     if (null != newValueINotifyCollectionChanged) 
     { 
      newValueINotifyCollectionChanged.CollectionChanged += new NotifyCollectionChangedEventHandler(newValueINotifyCollectionChanged_CollectionChanged); 
     } 
    } 

    void newValueINotifyCollectionChanged_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) 
    { 
     //Do your stuff here. 

    } 

    public IEnumerable ItemSourrce 
    { 
     get { return (IEnumerable)this.GetValue(ItemSourceProperty); } 
     set { this.SetValue(ItemSourceProperty, value); } 
    } 
public void TextFiltering(ICollectionView filteredview, DevExpress.Xpf.Editors.TextEdit textBox) 
    { 
     string filterText = ""; 
     filteredview.Filter = delegate(object obj) 
     { 

      if (String.IsNullOrEmpty(filterText)) 
      { 
       return true; 
      } 
      string str = obj as string; // Problem is here. 
             // I need to cast obj to my ItemSourrce Data Type. 
      if (String.IsNullOrEmpty(obj.ToString())) 
      { 
       return true; 
      } 

      int index = str.IndexOf(filterText, 0, StringComparison.InvariantCultureIgnoreCase); 
      return index > -1; 
     }; 
     textBox.EditValueChanging += delegate 
     { 
      filterText = textBox.Text; 
      filteredview.Refresh(); 
     }; 
    } 

    private void textEdit1_GotFocus(object sender, RoutedEventArgs e) 
    { 

     ICollectionView view = CollectionViewSource.GetDefaultView(ItemSourrce); 
     TextFiltering(view, textEdit1); 
    } 

この使用コントロールを呼び出す:ここに私がしようと私のコードです

List<testClass> myList = new List<testClass>(); 
    public void testMethod() 
    { 
     for (int i = 0; i < 20; i++) 
     { 
      myList.Add(new testClass { testData=i.ToString()}); 
     } 
     myTestControl.ItemSourrce = myList; 
    } 

    public class testClass 
    { 
     public string testData { get; set; } 
    } 

thank`sが あなたは(一貫性の命名かのItemsSource)ItemSourceにASSINGしたいもの

答えて

0

だけでなくすべてのものIEnumerableインターフェイスを実装する必要があります。 Thistはforeachを使って反復することができる基本的なことを意味します(簡単に言えば)。したがって、すべてのリスト、配列、コレクション、辞書など。

通常、あなたは普通の箱の文字列をスローすることはできません!

オブジェクトが本当に文字列(デバッグ時にブレークポイントでチェック)の場合は、何とか分割する必要があります。

PS:()明示的ToStirng(空の文字列を返すいくつかのまれなunmeaningfull例を除く)の方法により、このコード

if (String.IsNullOrEmpty(obj.ToString())) 
{ 
    return true; 
} 

決してobj.ToString()がnullまたは空になることはありませんので、trueを返します。 Object型の標準実装(各.net Typeが基になっています)では、名前空間と型名を返します。

0

私はあなたのオブジェクトがtestClassのタイプであり、stringではないことが分かりました。

あなたはコードをキャストしようとする必要があります。

string str = string.Empty; 

testClass myObject = obj as testClass; 

if (myObject == null) 
    return true; 
else 
    str = myObject.testData; 

if (string.IsNullOrEmpty(str)) 
    return true; 

return str.IndexOf(filterText, 0, StringComparison.InvariantCultureIgnoreCase) > -1; 
+0

はい私はあなたに同意します。 しかし、もし私が置くmyTestControl1.ItemSourrce = myList1; およびmyTestControl2.ItemSourrce = myList2; それから何が起こるでしょうか?私はリストボックスのような私のコントロールを使用したい。 –

+0

@ Hasan009両方のItemsSourceがtestClass型である限り、正常に動作します。毎回特定のビューにフィルターを割り当てるためです。すべてのビューに対して同じフィルタではありません。 – Dummy01

+0

実際に私は自分のItemSourrceのタイプを取得したい。この場合、データ型はtestClassです。いくつかのプロジェクトでコントロールを使用したいので、データソースが変更されます。 –

関連する問題