2012-04-29 90 views
0

我想開發一個自定義用戶控件。在我的用戶控件中,我使用兩個控件列表框和文本框。文本框用於過濾列表框中的項目。爲此,我在過濾方法中遇到問題。在我的過濾器方法中,我需要將對象投射到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

要assing到的ItemSource(或ItemsSource時命名的一致性)什麼

回答

0

好一切必須實現IEnumerable接口。這意味着基本上你可以通過foreach進行迭代(簡單地說)。所以每個List,數組,集合,字典等等。

通常你不能迭代通過一個正常的框字符串!

所以如果你的對象是一個字符串(在調試時用一個斷點檢查它),你必須以某種方式分割它。

PS:因爲obj.ToString()是不能爲null或空的方式驗證碼

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

從來沒有(除了某些罕見unmeaningfull例如當你明確地返回一個空字符串ToStirng())返回true 。在Object類型的標準實現中(每個.NET類型都基於它),它返回名稱空間和類型名稱。

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; 那麼會發生什麼?我想用我的控件,如列表框。 – 2012-04-29 06:03:24

+0

@ Hasan009只要兩個ItemsSource都是testClass類型,它就會正常工作。因爲您每次都將過濾器分配給特定的視圖。對於你所有的觀點來說,這不是同一個過濾器。 – Dummy01 2012-04-29 06:10:58

+0

其實我想要得到我的ItemSourrce的類型。在這種情況下,數據類型是testClass.I想要在幾個項目中使用我的控件,因此數據源將發生更改。 – 2012-04-29 07:13:59

相關問題